简体   繁体   English

这是否真的是最实用的方法来返回R中线性模型(lm)对象的p值?

[英]Is this really the most practical way to return the p-value of a linear model (lm) object in R?

What is the most practical way of extracting the global p-value of a linear model, lm ? lm提取线性模型的全局p值最实用的方法是什么? I usually end up taking the results from summary and plugging the F-test statistic and degrees of freedom into pf : 我通常最终将结果summary并将F检验统计量和自由度插入到pf

set.seed(1)
n <- 10
x <- 1:10
y <- 2*x+rnorm(n)
fit <- lm(y ~ x)
summary(fit) # global p-value: 1.324e-08
fstat <- summary(fit)$fstat
pval <- pf(fstat[1], fstat[2], fstat[3], lower.tail = FALSE)
pval

Check out the broom package: 检查扫帚包:

library(broom)

set.seed(1)
n <- 10
x <- 1:10
y <- 2*x+rnorm(n)
fit <- lm(y ~ x)

glance(fit)
#   r.squared adj.r.squared     sigma statistic      p.value df    logLik      AIC      BIC deviance df.residual
# 1 0.9851881     0.9833366 0.8090653  532.1048 1.324022e-08  2 -10.95491 27.90982 28.81758 5.236693           8

glance(fit)$p.value
# [1] 1.324022e-08

tidy(fit)
#          term   estimate  std.error  statistic      p.value
# 1 (Intercept) -0.1688236 0.55269681 -0.3054542 7.678170e-01
# 2           x  2.0547321 0.08907516 23.0673979 1.324022e-08

Since you asked for it: 既然你要求它:

Here is a bare-bones implementation that omits the bells and whistles (and checks) of lm . 这是一个简单的实现,省略了lm的钟声和口哨(和检查)。 As a consequence it is faster, but you'd use it at your own risk, ie, the warnings in help("lm.fit") apply. 因此它更快,但您使用它需要您自担风险,即help("lm.fit")的警告help("lm.fit")适用。 Due to laziness, code for calculation of the F-stats was extracted from the summary.lm source code and only slightly amended (so please consider licence() and citation("stats") ). 由于懒惰,计算F-stats的代码是从summary.lm源代码中提取的,只是稍作修改(因此请考虑licence()citation("stats") )。

fit1 <- lm.fit(cbind(1, x), y)

fstats <- function(obj) {
  p <- obj$rank
  rdf <- obj$df.residual
  r <- obj$residuals
  f <- obj$fitted.values
  mss <-  sum((f - mean(f))^2)
  rss <- sum(r^2)
  resvar <- rss/rdf
  df.int <- 1L #assumes there is always an intercept
  fstatistic <- c(value = (mss/(p - df.int))/resvar, 
                      numdf = p - df.int, dendf = rdf)
  fstatistic["pval"] <- pf(fstatistic[1L], 
                           fstatistic[2L], 
                           fstatistic[3L], lower.tail = FALSE)
  fstatistic
}

fstats(fit1)
#       value        numdf        dendf         pval 
#5.321048e+02 1.000000e+00 8.000000e+00 1.324022e-08 

Check the source of print.summary.lm, it uses the pf function to get the pvalue. 检查print.summary.lm的来源,它使用pf函数来获取pvalue。

 format.pval(pf(x$fstatistic[1L], 
            x$fstatistic[2L], x$fstatistic[3L], lower.tail = FALSE), 
            digits = digits))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM