我正在运行许多回归,并且只对一个特定变量的系数和 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 值。是否有可能做到这一点?
谢谢!
最佳答案
你要
coef(summary(fit))[,4]
它从
summary(fit)
显示的表格输出中提取 p 值的列向量.直到您运行 summary()
才实际计算 p 值在模型拟合上。顺便说一句,如果可以,请使用提取器函数而不是深入研究对象:
fit$coefficients[2]
应该
coef(fit)[2]
如果没有提取器功能,
str()
是你的 friend 。它允许您查看任何对象的结构,这使您可以查看对象包含什么以及如何提取它: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"
因此我们注意到
coefficients
我们可以使用 coef()
提取的组件,但其他组件没有提取器,例如 null.deviance
,您可以将其提取为 summ$null.deviance
.
关于r - 从 glm 中提取 pvalue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23838937/