【问题标题】:Extracting element from list [duplicate]从列表中提取元素[重复]
【发布时间】:2018-08-30 17:17:21
【问题描述】:
fit <- lm(R1 ~ R2)

产量...

Call:
lm(formula = R1 ~ R2)

Residuals:
      Min        1Q    Median        3Q       Max 
-0.128139 -0.016528  0.000349  0.017806  0.125109 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.003785   0.001884   2.009   0.0453 *  
R.2         0.949923   0.043904  21.636   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

我对这些结果进行了线性回归。现在我想收集一个值,更准确地说:fit["coefficients" -> "(Intercept)"]。

我试试

fit["coefficients"]

产生

$coefficients
(Intercept)   R2 
0.003785144 0.949923219

如何提取这个 1 值?

提前谢谢你!

【问题讨论】:

  • 试试这个:fit$coefficients[[1]]summary(fit)$coef[1,1]
  • 成功了!谢谢!!!
  • fit$coefficients[1] 也可以工作,尽管结果会被命名。您还可以使用递归索引fit[[c("coefficients", "(intercept)")]]
  • @lmo 第二个例子应该是 ..."(Intercept)" .... 很抱歉分叉

标签: r


【解决方案1】:

函数coef() 允许从lm 对象中提取系数。因此,要获得截距值,您只需使用

coef(fit)[1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2022-07-09
    • 2019-06-28
    • 1970-01-01
    • 2018-11-18
    • 2018-01-24
    • 2020-11-04
    相关资源
    最近更新 更多