R中多元线性回归模型中预测变量影响的估计量方差估计

时间:2020-03-08 20:02:22

标签: r regression lm

    bweight   gestwks            hyp sex    

1    2974 38.5200004577637       0 female          
2    3270 NA                     0 male            
3    2620 38.150001525878899     0 female          
4    3751 39.799999237060497     0 male            
5    3200 38.889999389648402     1 male           
6    3673 40.970001220703097     0 female          

bweight =婴儿体重

gestwks =每周妊娠期

hyp =母体高血压的存在

sex =婴儿的性别

我没有这个示例,并且使用以下代码创建了一个多元线性回归模型:

lm2 = lm(bweight ~ gestwks + hyp + male)

其中,男性和女性是男性的1和0的向量。

如何找到误差sigma ^ 2的方差的无偏估计?代码是:

summary(lm2)$sigma^2

要给我我想要的答案吗?

此外,我如何找到高血压效应估算器的方差估算值。

所以,说我有高血压的存在会影响婴儿体重-200(即,高血压每增加1单位,平均体重就会减少200)。高血压效应的估计量方差的估计值是什么?

1 个答案:

答案 0 :(得分:1)

您的示例数据:

df = structure(list(bweight = c(2974L, 3270L, 2620L, 3751L, 3200L, 
3673L), gestwks = c(38.5200004577637, NA, 38.1500015258789, 39.7999992370605, 
38.8899993896484, 40.9700012207031), hyp = c(0L, 0L, 0L, 0L, 
1L, 0L), sex = structure(c(1L, 2L, 1L, 2L, 2L, 1L), .Label = c("female", 
"male"), class = "factor"), male = c(0, 1, 0, 1, 1, 0)), row.names = c("1", 
"2", "3", "4", "5", "6"), class = "data.frame")

df$male = as.numeric(df$sex=="male")
lm2 = lm(bweight ~ gestwks + hyp + male,data=df)

您想要的是方差-协方差矩阵:

vcov(lm2)
            (Intercept)     gestwks         hyp       male
(Intercept)   8615153.6 -219476.110 -199723.227 119995.418
gestwks       -219476.1    5596.976    5093.248  -3283.549
hyp           -199723.2    5093.248   57215.841 -29278.523
male           119995.4   -3283.549  -29278.523  36980.334

对角线是每个估计量的方差,如果取平方根,则会得到显示为摘要的标准误差:

sqrt(diag(vcov(lm2)))
(Intercept)     gestwks         hyp        male 
 2935.15819    74.81294   239.19833   192.30271 

summary(lm2)

Call:
lm(formula = bweight ~ gestwks + hyp + male, data = df)

Residuals:
         1          3          4          5          6 
 1.218e+02 -1.058e+02  0.000e+00  7.105e-15 -1.598e+01 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept) -10304.13    2935.16  -3.511    0.177
gestwks        341.55      74.81   4.565    0.137
hyp           -240.19     239.20  -1.004    0.499
male           461.63     192.30   2.401    0.251