从glm中提取p值

53

我正在运行多个回归,并且只对一个特定变量的系数和p值感兴趣。因此,在我的脚本中,我希望能够从glm摘要中提取p值(获取系数本身很容易)。我知道查看p值的唯一方法是使用summary(myReg)。还有其他方法吗?

fit <- glm(y ~ x1 + x2, myData)
x1Coeff <- fit$coefficients[2] # only returns coefficient, of course
x1pValue <- ???

我已经尝试将fit$coefficients视为矩阵,但仍然无法简单地提取p值。

这是否可能实现?

谢谢!

5个回答

74
coef(summary(fit))[,4]

summary(fit)所显示的表格输出中提取列向量p值。直到在模型拟合上运行summary()之前,p值实际上并没有被计算出来。

顺便说一句,如果可以,请使用提取函数而不是深入对象:

fit$coefficients[2]

应该是

coef(fit)[2]

如果没有提取函数,str()会是你的好朋友。它可以让你查看任何对象的结构,从而让你看到对象包含什么以及如何提取它:

summ <- summary(fit)

> str(summ, max = 1)
List of 17
 $ call          : language glm(formula = counts ~ outcome + treatment, family = poisson())
 $ terms         :Classes 'terms', 'formula' length 3 counts ~ outcome + treatment
  .. ..- attr(*, "variables")= language list(counts, outcome, treatment)
  .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. ..- attr(*, "term.labels")= chr [1:2] "outcome" "treatment"
  .. ..- attr(*, "order")= int [1:2] 1 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(counts, outcome, treatment)
  .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "factor" "factor"
  .. .. ..- attr(*, "names")= chr [1:3] "counts" "outcome" "treatment"
 $ family        :List of 12
  ..- attr(*, "class")= chr "family"
 $ deviance      : num 5.13
 $ aic           : num 56.8
 $ contrasts     :List of 2
 $ df.residual   : int 4
 $ null.deviance : num 10.6
 $ df.null       : int 8
 $ iter          : int 4
 $ deviance.resid: Named num [1:9] -0.671 0.963 -0.17 -0.22 -0.956 ...
  ..- attr(*, "names")= chr [1:9] "1" "2" "3" "4" ...
 $ coefficients  : num [1:5, 1:4] 3.04 -4.54e-01 -2.93e-01 1.34e-15 1.42e-15 ...
  ..- attr(*, "dimnames")=List of 2
 $ aliased       : Named logi [1:5] FALSE FALSE FALSE FALSE FALSE
  ..- attr(*, "names")= chr [1:5] "(Intercept)" "outcome2" "outcome3" "treatment2" ...
 $ dispersion    : num 1
 $ df            : int [1:3] 5 4 5
 $ cov.unscaled  : num [1:5, 1:5] 0.0292 -0.0159 -0.0159 -0.02 -0.02 ...
  ..- attr(*, "dimnames")=List of 2
 $ cov.scaled    : num [1:5, 1:5] 0.0292 -0.0159 -0.0159 -0.02 -0.02 ...
  ..- attr(*, "dimnames")=List of 2
 - attr(*, "class")= chr "summary.glm"
因此,我们注意到可以使用coef()提取的coefficients组件,但是其他组件没有提取器,例如null.deviance,您可以将其提取为summ$null.deviance

1
你在我寻找重复内容的时候就已经击败了我(虽然没有完美的重复内容,但是有很多关于从 [g]lm 拟合中提取信息的帖子,例如:http://stackoverflow.com/questions/12496368/how-to-extract-tabular-summary-data-from-an-lm-command-in-r)。 - Ben Bolker
3
如果你不知道访问器有哪些可用,必须自行挖掘对象来获取信息,那么在使用str()时最好添加一条注释。 - Ben Bolker
1
实际上,我使用str()来尝试找出如何获取数据,但是我没有看到在str()中可以推断出我需要coef()函数来获取我要查找的内容。我正在阅读您的更新,但我还是没有看到… - ch-pub
3
了解coef的方法是执行methods(class="lm")methods(class="summary.lm")。我同意你无法从查看str()中发现可以使用coef() - Ben Bolker
1
@Clark 看一下 class(fit),你会发现 glm 拟合继承自类 "lm",所以你需要寻找该类的方法。 - Gavin Simpson
显示剩余8条评论

12

您可以直接使用名称代替数字

coef(summary(fit))[,'Pr(>|z|)']

以下是系数摘要中提供的其他信息:

估计值 标准误差 z值 Pr(>|z|)


4

过去我曾经使用这种技术从 summary 或拟合模型对象中提取预测数据:

coef(summary(m))[grepl("var_i_want$",row.names(coef(summary(m)))), 4]

这让我能够轻松地编辑我想要获取数据的变量。

或者像 @Ben 指出的那样,使用 match%in%,相比 grepl 更为简洁:

coef(summary(m))[row.names(coef(summary(m))) %in% "var_i_want" , 4]

1
或者使用 match() 函数,或者适当地索引行。 - Ben Bolker

3
tidy 函数来自 broom 包(Tidyverse 的一部分,可在 CRAN 上获取),提供了一种快速简便的方法将 GLM 摘要转换为数据框,在除上述情况外的许多其他情况下可能会有用。
在这种情况下,您可以使用以下代码获得所需的输出:
x1pValue <- broom::tidy(fit)$p.value[2]

1

好的,这是另一种方式,但不是最有效的方式来执行它:

a = coeftable(model).cols[4]
pVals = [ a[i].v for i in 1:length(a) ]

这确保了从glm中提取的值不在StatsBase中。 因此,您可以根据需要调整pVals。 希望对您有所帮助, Ebby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接