从glm中提取pvalue

Cla*_*nry 32 r glm p-value

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

例如:

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

我曾尝试将其fit$coefficients作为矩阵处理,但我仍然无法简单地提取p值.

是否有可能做到这一点?

谢谢!

Rei*_*son 53

你要

coef(summary(fit))[,4]
Run Code Online (Sandbox Code Playgroud)

它从表格所示的表格输出中提取p值的列向量summary(fit).在您运行模型拟合之前,实际上不会计算p值summary().

顺便说一下,如果可以的话,使用提取器函数而不是深入研究对象:

fit$coefficients[2]
Run Code Online (Sandbox Code Playgroud)

应该

coef(fit)[2]
Run Code Online (Sandbox Code Playgroud)

如果没有提取器功能,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"
Run Code Online (Sandbox Code Playgroud)

因此我们注意到coefficients我们可以使用提取的组件coef(),但是其他组件没有提取器,例如null.deviance,您可以提取为summ$null.deviance.

  • 当你不知道哪些访问器可用时,可能值得在`str()`上添加注释,并且必须自己尝试从对象中挖掘东西. (3认同)
  • 找出`coef`的方法是做`methods(class ="lm")`或`methods(class ="summary.lm")`.我同意你不能从查看`str()`中弄清楚你可以使用`coef()`. (3认同)

Jam*_*ing 5

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

coef(summary(m))[grepl("var_i_want$",row.names(coef(summary(m)))), 4]
Run Code Online (Sandbox Code Playgroud)

这让我可以轻松编辑想要获取数据的变量。

或者正如 @Ben 所指出的,使用match%in%,比 更干净一些grepl

coef(summary(m))[row.names(coef(summary(m))) %in% "var_i_want" , 4]
Run Code Online (Sandbox Code Playgroud)


R. *_*ost 5

您可以直接输入名称而不是数字

coef(summary(fit))[,'Pr(>|z|)']
Run Code Online (Sandbox Code Playgroud)

系数摘要中的其他可用参数:

Estimate Std. Error z value Pr(>|z|)