wald.test:L%*%V中的错误:不一致的参数

时间:2015-04-27 05:28:33

标签: r

当我尝试在线性模型上的分类变量上运行wald.test(来自aod包)时,出现以下错误:

Error in L %*% V : non-conformable arguments

我遇到问题的代码:

m1 <- glm(comment_count ~ factor(has_conflicts) + factor(base_repo_id) + **snip**, data = mydata)
summary(m1) # shows that base_repo_id's factors are coefficients 3 through 12

# Determine whether base_repo_id matters
wald.test(b = coef(m1), Sigma = vcov(m1), Terms = 3:12)

据我了解,wald.test b参数是线性回归系数,Sigma是回归的方差,{\ n} {1}}选择我想要运行Wald测试的变量。那么为什么我会收到错误?

2 个答案:

答案 0 :(得分:2)

原则上,您的代码看起来不错,但它必须与您的数据特别适合无法正常工作。可能存在未识别参数或奇异协方差矩阵等问题?

如果我用上面的变量创建一个随机数据集,那么一切都顺利进行:

set.seed(1)
mydata <- data.frame(
  comment_count = rpois(500, 3),
  has_conflicts = sample(0:1, 500, replace = TRUE),
  base_repo_id = sample(1:11, 500, replace = TRUE)
)    
m1 <- glm(comment_count ~ factor(has_conflicts) + factor(base_repo_id),
  data = mydata)

Wald测试可以由基础R的anova()(在高斯情况下等同于Wald测试)进行:

m0 <- update(m1, . ~. - factor(base_repo_id))
anova(m0, m1, test = "Chisq")
## Analysis of Deviance Table
## 
## Model 1: comment_count ~ factor(has_conflicts)
## Model 2: comment_count ~ factor(has_conflicts) + factor(base_repo_id)
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       498     1426.1                     
## 2       488     1389.2 10    36.91   0.2256

或者您可以使用aod

library("aod")
wald.test(b = coef(m1), Sigma = vcov(m1), Terms = 3:12)               
## Wald test:
## ----------
## 
## Chi-squared test:
## X2 = 13.0, df = 10, P(> X2) = 0.23

lmtest

library("lmtest")
waldtest(m1, "factor(base_repo_id)", test = "Chisq")     
## Wald test
## 
## Model 1: comment_count ~ factor(has_conflicts) + factor(base_repo_id)
## Model 2: comment_count ~ factor(has_conflicts)
##   Res.Df  Df  Chisq Pr(>Chisq)
## 1    488                      
## 2    498 -10 12.966     0.2256

car

library("car")
linearHypothesis(m1, names(coef(m1))[3:12])
## Linear hypothesis test
## 
## Hypothesis:
## factor(base_repo_id)2 = 0
## factor(base_repo_id)3 = 0
## factor(base_repo_id)4 = 0
## factor(base_repo_id)5 = 0
## factor(base_repo_id)6 = 0
## factor(base_repo_id)7 = 0
## factor(base_repo_id)8 = 0
## factor(base_repo_id)9 = 0
## factor(base_repo_id)10 = 0
## factor(base_repo_id)11 = 0
## 
## Model 1: restricted model
## Model 2: comment_count ~ factor(has_conflicts) + factor(base_repo_id)
## 
##   Res.Df Df  Chisq Pr(>Chisq)
## 1    498                     
## 2    488 10 12.966     0.2256

答案 1 :(得分:0)

我遇到了同样的问题。 该错误表明两个矩阵L和V的大小不匹配。

请检查系数中是否有NA元素。 vcov()会自动删除NA元素,这会改变矩阵的大小,使其大小不匹配。