You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中提取线性回归模型Estimate向量并降序取前5值

提取线性回归系数Estimate并取Top5降序值

步骤1:提取Estimate向量

从回归模型的系数矩阵中直接提取Estimate列——summary(model)$coefficients返回的是包含多列(Estimate、Std. Error等)的矩阵,通过列名索引即可获取目标向量:

# 提取Estimate向量
estimate_vals <- summary(model)$coefficients[, "Estimate"]

步骤2:降序排序并取最大5个值

用sort()函数设置decreasing = TRUE实现降序排列,再通过head()截取前5个结果:

# 降序排序后取Top5
top5_estimates <- head(sort(estimate_vals, decreasing = TRUE), n = 5)

# 查看最终结果
top5_estimates

简化成单行代码(可选)

如果想精简代码,可以直接把两步合并:

top5_estimates <- head(sort(summary(model)$coefficients[, "Estimate"], decreasing = TRUE), 5)

最终结果会保留自变量名称作为向量的名称,方便你直接对应查看哪个自变量的系数最大。

内容的提问来源于stack exchange,提问作者Bosser445

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 03:47:02