对多个线性回归列表进行的Wald检验

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

使用此处显示的lm使用新创建的69个模型的列表:Looping through many multiple regressions

我正在尝试进行Wald测试,但似乎无法同时在69个模型上运行。仅当我指定对列表中的一种型号进行Wald测试时,它才有效。关于如何通过比较系数1和2获得总体Wald检验的任何想法?

install.packages("aod") 
library(aod) 
wald.test(b = coef(models[[1]]), Sigma = vcov(models[[1]]), Terms = 1:2) #this one works only 
wald.test(b = coef(models), Sigma = vcov(models,Terms = 1:2)) #this one does not work
r linear-regression hypothesis-test
2个回答
0
投票

您将必须使用lapply遍历69个模型中的每一个,并将wald.test函数分别应用于每个模型。当应用于69个型号中的每个型号时,result将是wald.test功能的输出列表。

尝试一下:

result = lapply(models,function(t){x = wald.test(b = coef(t), Sigma = vcov(t,Terms = 1:2)); x})

0
投票

实际上,您可以使用other question的解决方案一步完成Wald测试。只需将$的Wald检验作为元素添加到模型输出中即可。 (请注意,我在这里使用的是reformulate(),而不是as.formula()。)>

n.ids <- 5
models <- lapply(1:n.ids, function(x) {
  fo <- reformulate(paste0("id", x, c(".PRE", ".POST")), response="growth_rate")
  fit <- lm(fo, output)
  fit$wald <- aod::wald.test(b=coef(fit), Sigma=vcov(fit), Terms=1:2)$result$chi2
  return(fit)
})

之后,您可以方便地提取模型拟合(此处显示为[[1]] st

模型),
models[[1]]
# Call:
#   lm(formula = fo, data = output)
# 
# Coefficients:
#   (Intercept)      id1.PRE     id1.POST  
#     0.0006867    0.3621801   -0.6805282

获取summary()统计信息,

summary(models[[1]])
# Call:
#   lm(formula = fo, data = output)
# 
# Residuals:
#   Min      1Q  Median      3Q     Max 
# -1.6332 -0.7522 -0.2040  0.3244  2.3479 
# 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)
# (Intercept)  0.0006867  0.4707891   0.001    0.999
# id1.PRE      0.3621801  0.7245488   0.500    0.632
# id1.POST    -0.6805282  0.4666070  -1.458    0.188
# 
# Residual standard error: 1.449 on 7 degrees of freedom
# Multiple R-squared:  0.2783,  Adjusted R-squared:  0.07208 
# F-statistic:  1.35 on 2 and 7 DF,  p-value: 0.3194

最后是Wald检验统计量:

models[[1]]$wald
#      chi2        df         P 
# 0.2602116 2.0000000 0.8780026

数据

output <- structure(list(growth_rate = c(-1.69916848732564, 0.580880770531803, 
-2.69289070966401, 1.35526046076076, -0.112984932782402, -0.462646659510964, 
0.719326790475801, -1.00142215628315, 2.47637095683882, 0.43656578200224
), id1.PRE = c(-0.658214571692787, 0.618757883859616, -1.00319408411405, 
-0.182500151494055, 0.900088109789603, 1.0196598975258, 0.412275530237581, 
0.392417622938614, -0.455987647372666, 0.102734423799357), id2.PRE = c(-0.170523375036079, 
0.686700233146119, 0.309985112368748, -1.90034614054823, -1.09227380863711, 
0.808287849081857, -0.499631929432237, 0.166532696960525, 0.302124067552245, 
0.387404901557692), id3.PRE = c(0.599764947203108, 0.89758476990722, 
-0.255477016875339, -0.356745234827032, 0.448024379737768, -0.256047154841515, 
0.585227234029825, -0.166333176678276, 0.823998488345663, -1.443644743169
), id4.PRE = c(-2.80604357500051, -1.09530031299481, 0.175000065646341, 
-0.392833839818346, -0.409796662979807, -0.195621477818208, -0.00577716957657597, 
-0.482953411765706, -2.02722251486943, 0.300223614906696), id5.PRE = c(-0.0622634352162204, 
0.040326767924506, 0.0497415705870156, 0.180785254746358, 1.33406548126792, 
0.0836709755262436, -0.73864218643137, -1.50512979963419, -0.515794856492169, 
-0.00132857851903661), id1.POST = c(1.31893320782006, -0.865022433359845, 
1.02422314119992, -1.5293010769327, 0.388204583082128, 0.026043363711214, 
1.91192868077842, -0.281576076956257, -0.430489296095949, -0.35410853516199
), id2.POST = c(-0.311676684306798, -0.368607205934452, -1.48144614489904, 
0.555325334696273, 0.570876384813286, -1.31976394074723, -0.612004459486653, 
1.22377107478453, 1.59353205696141, -0.690394114816965), id3.POST = c(-0.720115908724216, 
-0.628303814726474, 0.861931079614817, 0.462347223839036, -1.03796670332245, 
-1.1879031168125, 0.26141696844815, -1.42623852487712, 0.538672447311226, 
-2.1422820318952), id4.POST = c(-1.43103098130001, 0.70344192904054, 
-0.599438467172651, -2.6958955065305, -0.140567798927688, 0.393778135755608, 
0.931307986770656, 0.107100043420371, 0.17241808370283, -0.334264159331698
), id5.POST = c(-0.468527805538746, -0.231147053156926, 1.16752987470932, 
-0.025411116476825, 0.377661302479598, 1.60646402608108, -0.142579077289651, 
0.269743622121994, -0.562298443852192, 1.41564052399418)), row.names = c(NA, 
-10L), class = "data.frame")
© www.soinside.com 2019 - 2024. All rights reserved.