使用 R 的逻辑回归

发布于 2024-12-02 15:51:21 字数 279 浏览 5 评论 0原文

我现在正在使用 R 运行逻辑回归,但我似乎无法获得许多有用的模型拟合统计数据。我正在寻找类似于 SAS 的指标:

http://www.ats .ucla.edu/stat/sas/output/sas_logit_output.htm

有谁知道我可以如何(或使用哪些包)来提取这些统计信息?

谢谢

I am running logistic regressions using R right now, but I cannot seem to get many useful model fit statistics. I am looking for metrics similar to SAS:

http://www.ats.ucla.edu/stat/sas/output/sas_logit_output.htm

Does anyone know how (or what packages) I can use to extract these stats?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虫児飞 2024-12-09 15:51:21

这是一个泊松回归示例:

## from ?glm:
d.AD <- data.frame(counts=c(18,17,15,20,10,20,25,13,12),
      outcome=gl(3,1,9),
      treatment=gl(3,3))
glm.D93 <- glm(counts ~ outcome + treatment,data = d.AD, family=poisson())

现在定义一个函数来拟合具有相同响应、族等的仅截距模型,计算汇总统计量,并将它们组合成一个表(矩阵)。下面的 update 命令中的公式 .~1 表示“使用相同的响应变量 [用波形符 LHS 上的点表示] 重新拟合模型,但仅使用截距项 [由波浪号右侧的 1 表示]”

glmsumfun <- function(model) {
   glm0 <- update(model,.~1)  ## refit with intercept only
   ## apply built-in logLik (log-likelihood), AIC,
   ##  BIC (Bayesian/Schwarz Information Criterion) functions
   ## to models with and without intercept ('model' and 'glm0');
   ## combine the results in a two-column matrix with appropriate
   ## row and column names
   matrix(c(logLik(glm.D93),BIC(glm.D93),AIC(glm.D93),
           logLik(glm0),BIC(glm0),AIC(glm0)),ncol=2,
     dimnames=list(c("logLik","SC","AIC"),c("full","intercept_only")))
}

现在应用该函数:

glmsumfun(glm.D93)

结果:

            full intercept_only
logLik -23.38066      -26.10681
SC      57.74744       54.41085
AIC     56.76132       54.21362

编辑

  • anova(glm.D93,test="Chisq") 对包含 df、偏差(=-2 对数似然)、残差 df、残差偏差的偏差表进行顺序分析,以及似然比检验(卡方检验)p-值。
  • drop1(glm.D93) 给出一个表格,其中包含每个单项删除的 AIC 值(df、偏差等); drop1(glm.D93,test="Chisq") 还给出了 LRT 测试 p 值。

Here's a Poisson regression example:

## from ?glm:
d.AD <- data.frame(counts=c(18,17,15,20,10,20,25,13,12),
      outcome=gl(3,1,9),
      treatment=gl(3,3))
glm.D93 <- glm(counts ~ outcome + treatment,data = d.AD, family=poisson())

Now define a function to fit an intercept-only model with the same response, family, etc., compute summary statistics, and combine them into a table (matrix). The formula .~1 in the update command below means "refit the model with the same response variable [denoted by the dot on the LHS of the tilde] but with only an intercept term [denoted by the 1 on the RHS of the tilde]"

glmsumfun <- function(model) {
   glm0 <- update(model,.~1)  ## refit with intercept only
   ## apply built-in logLik (log-likelihood), AIC,
   ##  BIC (Bayesian/Schwarz Information Criterion) functions
   ## to models with and without intercept ('model' and 'glm0');
   ## combine the results in a two-column matrix with appropriate
   ## row and column names
   matrix(c(logLik(glm.D93),BIC(glm.D93),AIC(glm.D93),
           logLik(glm0),BIC(glm0),AIC(glm0)),ncol=2,
     dimnames=list(c("logLik","SC","AIC"),c("full","intercept_only")))
}

Now apply the function:

glmsumfun(glm.D93)

The results:

            full intercept_only
logLik -23.38066      -26.10681
SC      57.74744       54.41085
AIC     56.76132       54.21362

EDIT:

  • anova(glm.D93,test="Chisq") gives a sequential analysis of deviance table containing df, deviance (=-2 log likelihood), residual df, residual deviance, and the likelihood ratio test (chi-squared test) p-value.
  • drop1(glm.D93) gives a table with the AIC values (df, deviances, etc.) for each single-term deletion; drop1(glm.D93,test="Chisq") additionally gives the LRT test p value.
乖乖哒 2024-12-09 15:51:21

当然,带有 family="binomial" 参数的 glm 是逻辑回归最常用的函数。因素对比的默认处理是不同的。 R 使用处理对比,SAS(我认为)使用总和对比。您可以在 R-help 上查找这些技术问题。在过去的十多年里,它们已经被讨论了很多很多次。

我看到 Greg Snow 在“rms”中提到了 lrm。它的优点是得到“rms”方法套件中其他几个函数的支持。我也会使用它,但是学习 rms 包可能需要一些额外的时间。我没有看到可以创建类似 SAS 输出的选项。

如果您想比较类似问题的软件包,UCLA StatComputing 页面还有另一个资源: http://www.ats.ucla.edu/stat/r/dae/default.htm,其中在SPSS、SAS、Stata和R中举例说明了大量方法。

Certainly glm with a family="binomial" argument is the function most commonly used for logistic regression. The default handling of contrasts of factors is different. R uses treatment contrasts and SAS (I think) uses sum contrasts. You can look these technical issues up on R-help. They have been discussed many, many times over the last ten+ years.

I see Greg Snow mentioned lrm in 'rms'. It has the advantage of being supported by several other functions in the 'rms' suite of methods. I would use it , too, but learning the rms package may take some additional time. I didn't see an option that would create SAS-like output.

If you want to compare the packages on similar problems that UCLA StatComputing pages have another resource: http://www.ats.ucla.edu/stat/r/dae/default.htm , where a large number of methods are exemplified in SPSS, SAS, Stata and R.

淡淡的优雅 2024-12-09 15:51:21

使用rms包中的lrm函数可能会给出您正在寻找的输出。

Using the lrm function in the rms package may give you the output that you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文