在扫帚结果中添加glm系数类别的列

时间:2016-08-29 02:17:17

标签: r glm broom

有没有办法在扫帚包的tidy函数的结果中添加一列,该函数可以将术语列与formula参数中使用的原始名称相关联以及data参数中的列。

例如,如果我运行以下内容,我会得到:

library(ggplot2)
library(dplyr)

mod <- glm(mpg ~ wt + qsec + as.factor(carb), data = mtcars)

tidy(mod)

#               term     estimate std.error   statistic      p.value
# 1      (Intercept) 21.132995090 7.5756463  2.78959633 1.017187e-02
# 2               wt -4.916303175 0.6747590 -7.28601380 1.584408e-07
# 3             qsec  0.843355538 0.3930252  2.14580532 4.221188e-02
# 4 as.factor(carb)2  0.004133826 1.5321134  0.00269812 9.978695e-01
# 5 as.factor(carb)3 -0.755346006 2.3451222 -0.32209239 7.501715e-01
# 6 as.factor(carb)4 -0.489721798 2.0628564 -0.23739985 8.143615e-01
# 7 as.factor(carb)6 -0.886846134 3.4443957 -0.25747510 7.990068e-01
# 8 as.factor(carb)8 -0.894783610 3.7496630 -0.23863041 8.134180e-01

我正在寻找的是这样的:

#               term     estimate std.error   statistic      p.value   term_base
# 1      (Intercept) 21.132995090 7.5756463  2.78959633 1.017187e-02 
# 2               wt -4.916303175 0.6747590 -7.28601380 1.584408e-07          wt
# 3             qsec  0.843355538 0.3930252  2.14580532 4.221188e-02        qsec
# 4 as.factor(carb)2  0.004133826 1.5321134  0.00269812 9.978695e-01        carb
# 5 as.factor(carb)3 -0.755346006 2.3451222 -0.32209239 7.501715e-01        carb
# 6 as.factor(carb)4 -0.489721798 2.0628564 -0.23739985 8.143615e-01        carb
# 7 as.factor(carb)6 -0.886846134 3.4443957 -0.25747510 7.990068e-01        carb
# 8 as.factor(carb)8 -0.894783610 3.7496630 -0.23863041 8.134180e-01        carb

如果此新列中的第一行为空Intercept1,则不会感到困扰。只需要一些能够将术语列与传递给公式的原始变量名匹配的东西吗?

修改

如果它不依赖于在公式中使用as.factor,那将会很好,例如可以继续:

mod <- glm(mpg ~ wt + qsec + carb, data = mtcars %>% mutate(carb = factor(carb)))

tidy(mod)

#          term     estimate std.error   statistic      p.value
# 1 (Intercept) 21.132995090 7.5756463  2.78959633 1.017187e-02
# 2          wt -4.916303175 0.6747590 -7.28601380 1.584408e-07
# 3        qsec  0.843355538 0.3930252  2.14580532 4.221188e-02
# 4       carb2  0.004133826 1.5321134  0.00269812 9.978695e-01
# 5       carb3 -0.755346006 2.3451222 -0.32209239 7.501715e-01
# 6       carb4 -0.489721798 2.0628564 -0.23739985 8.143615e-01
# 7       carb6 -0.886846134 3.4443957 -0.25747510 7.990068e-01
# 8       carb8 -0.894783610 3.7496630 -0.23863041 8.134180e-01

1 个答案:

答案 0 :(得分:2)

我们可以使用正则表达式创建'term_base'列

tidy(mod) %>%
        mutate(term_base = sub("Intercept", "", gsub(".*\\(|\\).*", "", term)))
#              term     estimate std.error   statistic      p.value term_base
#1      (Intercept) 21.132995090 7.5756463  2.78959633 1.017187e-02          
#2               wt -4.916303175 0.6747590 -7.28601380 1.584408e-07        wt
#3             qsec  0.843355538 0.3930252  2.14580532 4.221188e-02      qsec
#4 as.factor(carb)2  0.004133826 1.5321134  0.00269812 9.978695e-01      carb
#5 as.factor(carb)3 -0.755346006 2.3451222 -0.32209239 7.501715e-01      carb
#6 as.factor(carb)4 -0.489721798 2.0628564 -0.23739985 8.143615e-01      carb
#7 as.factor(carb)6 -0.886846134 3.4443957 -0.25747510 7.990068e-01      carb
#8 as.factor(carb)8 -0.894783610 3.7496630 -0.23863041 8.134180e-01      carb

如果我们在as.factor步骤之前mutate将'碳水化合物'factor移至glm,那么mtcars %>% mutate(carb = factor(carb)) %>% glm(formula = mpg ~wt + qsec + carb, data = .) %>% tidy(.) %>% mutate(term_base = sub("\\(.*\\)|\\d+", "", term)) # term estimate std.error statistic p.value term_base #1 (Intercept) 21.132995090 7.5756463 2.78959633 1.017187e-02 #2 wt -4.916303175 0.6747590 -7.28601380 1.584408e-07 wt #3 qsec 0.843355538 0.3930252 2.14580532 4.221188e-02 qsec #4 carb2 0.004133826 1.5321134 0.00269812 9.978695e-01 carb #5 carb3 -0.755346006 2.3451222 -0.32209239 7.501715e-01 carb #6 carb4 -0.489721798 2.0628564 -0.23739985 8.143615e-01 carb #7 carb6 -0.886846134 3.4443957 -0.25747510 7.990068e-01 carb #8 carb8 -0.894783610 3.7496630 -0.23863041 8.134180e-01 carb 也可以从“期限”中删除

<android.support.v7.widget.AppCompatButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginBottom="24dp"
            android:padding="12dp"
            android:text="@string/signin"
            android:onClick="@{account::onSignInButtonClick}"/>