我正在运行许多回归,我只对一个特定变量的系数和p值的影响感兴趣。因此,在我的脚本中,我希望能够从glm摘要中提取p值(获得系数本身很容易)。我所知道的查看p值的唯一方法是使用汇总(MyReg)。还有别的办法吗?
例如:
fit <- glm(y ~ x1 + x2, myData)
x1Coeff <- fit$coefficients[2] # only returns coefficient, of course
x1pValue <- ???我尝试过将fit$coefficients看作一个矩阵,但仍然不能简单地提取p值。
有可能这样做吗?
谢谢!
发布于 2014-05-23 22:05:49
你想要的
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"因此,我们注意到可以使用coefficients提取coef()组件,但其他组件没有提取器,比如null.deviance,您可以将其提取为summ$null.deviance。
发布于 2018-01-24 09:22:46
而不是数字,你可以直接把名字
coef(summary(fit))[,'Pr(>|z|)']从系数摘要中可获得的其他资料如下:
Estimate Std. Error z value Pr(>|z|)
发布于 2014-05-23 22:18:28
我过去曾使用这种技术从summary或拟合模型对象中提取预测器数据:
coef(summary(m))[grepl("var_i_want$",row.names(coef(summary(m)))), 4]这让我可以轻松地编辑我想要获取数据的变量。
或者如be @Ben所指出的,使用match或%in%,比grepl稍微干净一些
coef(summary(m))[row.names(coef(summary(m))) %in% "var_i_want" , 4]https://stackoverflow.com/questions/23838937
复制相似问题