使用类型 2 平方和的线性回归,如何从模型输出中提取斜率、截距、r2?

问题描述 投票:0回答:1

我想知道如何使用类型 2 平方和从线性回归的输出中提取斜率、截距和 R2?

我知道可以使用 R 包 car 执行“II 型”平方和。例如,

df <- data.frame(x = c(1, 2, 3, 4, 5),
                   y = c(2, 4, 6, 8, 10.5))
model <- lm(y ~ x, data = df)
anova_result <- car::Anova(model, type="II")

它给出的输出就像只有 Sq、Df 和 P 值的总和 Anova 表(II 型测试),没有斜率、截距和 r2。

r linear-regression
1个回答
0
投票

获取模型参数和整体 R2 的一种方法是使用基函数

summary()
。在您的情况下,斜率约为 2.1,截距 - 0.2,倍数 R2 为 0.977.

> summary(model)

Call:
lm(formula = y ~ x, data = df)

Residuals:
         1          2          3          4          5 
 1.000e-01  8.285e-17 -1.000e-01 -2.000e-01  2.000e-01 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -0.20000    0.19149  -1.044    0.373    
x            2.10000    0.05774  36.373 4.57e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1826 on 3 degrees of freedom
Multiple R-squared:  0.9977,    Adjusted R-squared:  0.997 
F-statistic:  1323 on 1 and 3 DF,  p-value: 4.57e-05

car 可让您根据如何划分总平方和来测试不同的方差分析假设,在此 交叉验证帖子 中总结得很好。

请注意,在这种情况下,

x
的主要效果的 F 和 p 值将与
type = "II"
type = "III"
相同。

© www.soinside.com 2019 - 2024. All rights reserved.