首页 > 解决方案 > R 是否提供构建方差分析表的能力,并通过控制变量对其进行拆分?

问题描述

我是 R 和 DataScience 的绿色同行,所以我需要你的帮助来回答一个非常简单的问题。

我正在尝试为下面的回归自定义 anova 表:

glm(car1~lnBTA:industry+prod:industry+BETA:industry+ROA:industry+age:industry, family = binomial(link = "logit"), data = df2)

如您所见,有一个控制变量“行业”,当我运行 summary() 时,我得到的结果如下所示:

Coefficients:
                         Estimate Std. Error z value Pr(>|z|)   
(Intercept)              3.335031   1.819230   1.833  0.06677 . 
lnBTA:industrybanking   -0.167698   0.134638  -1.246  0.21293   
lnBTA:industryinsurance  0.231744   0.345148   0.671  0.50194   
industrybanking:prod    -0.810295   1.765389  -0.459  0.64624   
industryinsurance:prod   0.623879   2.939748   0.212  0.83193   
industrybanking:BETA    -0.576087   0.606819  -0.949  0.34244   
industryinsurance:BETA  -4.389630   1.455596  -3.016  0.00256 **
industrybanking:ROA      0.003612   0.040308   0.090  0.92860   
industryinsurance:ROA   -0.158213   0.089857  -1.761  0.07829 . 
industrybanking:age     -0.002211   0.003730  -0.593  0.55330   
industryinsurance:age   -0.015225   0.014741  -1.033  0.30169

我想查看方差分析表,其中变量有 5 行,结果有 8 个列,每个行业 4 个。我怎么才能得到它?

标签: rsummary

解决方案


R 是否提供构建方差分析表的能力,并通过控制变量对其进行拆分?

是的,它确实。

我怎样才能得到它(变量 5 行,结果 8 列,每个行业 4 个)?

你可以像这样得到它:

library(stringr)
mod <- glm(car1~lnBTA:industry+prod:industry+BETA:industry+ROA:industry+age:industry, family = binomial(link = "logit"), data = df2)
allres <- as.data.frame(summary(mod)$coefficients)
allres$names1 <- as.character(rownames(allres))
allres$names1[1] <- "Intercept:Intercept"
allres$names1[2] <- "industrybanking:lnBTA"   
allres$names1[3] <- "industryinsurance:lnBTA"

allres$names2 <- str_split_fixed(allres$names1, ":", 2)[,1]
allres$names3 <- str_split_fixed(allres$names1, ":", 2)[,2]
rownames(allres) <- NULL

pt1res <- allres[1,]     # new dataframe with intercept - if you need it for something later
pt2res <- allres[c(2,4,6,8,10),] # new dataframe with results for industrybanking   
pt3res <- allres[c(3,5,7,9,11),] # new dataframe with results for industryinsurance   


newres <- merge(pt2res, pt3res, by="names3") # merging the two dataframes
newres

推荐阅读