从R中的glht中提取t-stat

时间:2013-10-29 15:56:39

标签: r lm

我在R中用glht进行了一个假设检验,我想提取测试的t-stat。我读到在我收到的类“glht”中应该有一个元素“test”,但是当我查看运行glht时得到的元素时,它不会出现。

代码非常简单,如下所示:

reg = lm(dep ~ indep)
htest = glht(reg,linfct = c("indep  = 0.5"))
names(htest)

运行最后一行给了我: [1]“model”“linfct”“rhs”“coef”“vcov”“df”“alternative”“type”

任何人都有答案吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

使用summary

indep <- 1:10
set.seed(42)
dep <- indep/2+5+rnorm(10)

reg = lm(dep ~ indep)

library(multcomp)
htest = glht(reg,linfct = c("indep  = 0.5"))
summary(htest)
#Simultaneous Tests for General Linear Hypotheses
#
#Fit: lm(formula = dep ~ indep)
#
#Linear Hypotheses:
#             Estimate Std. Error t value Pr(>|t|)
#indep == 0.5  0.53040    0.09697   0.313    0.762
#(Adjusted p values reported -- single-step method)

您可以像这样提取值:

res <- summary(htest)
res$test[-(1:2)]
# $coefficients
# indep 
# 0.5303967 
# 
# $sigma
# indep 
# 0.09696568 
# 
# $tstat
# indep 
# 0.3134785 
# 
# $pvalues
# [1] 0.7619346
# attr(,"error")
# [1] 0
# 
# $type
# [1] "single-step"