正确将系数绑定到汇总表 [英] Correctly binding coefficients to summarized table
问题描述
我有一个 glm
模型和一个汇总数据集,要求我绑定系数
,标准误差
和 p.value
。例如,我使用了 mtcars
数据集。我在最终的联合数据集中添加了列
来模拟我想要的系数,标准误差和p值
将被置于。对于模型中未显示的基本值,我想在系数 $ c $中添加
1
c>并使用截距,标准误差和p值
。我该怎么办?
I have a glm
model and a summarized dataset that requires I bind the coefficients
, standard error
and p.value
from the summary of the model to the summarized dataset. For an example, I used the mtcars
data set. I added columns
to the final unioned data set to mimic where I would like coefficients, standard errors, and p-values
to be placed. In terms of the base values that aren't shown in the model, I would like to add a "1
" to coefficients
and use the intercept, standard errors and p-value
. How could I do all of this?
library(tidyverse)
mtcars <- as_tibble(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)
#run model
model1 <- glm(mpg ~ cyl + gear, data = mtcars)
summary(model1)
#start developing summarized data set
mtcars_wght <- mtcars %>%
group_by(cyl) %>%
rename(level = cyl) %>%
summarise("sum_weight" = sum(wt)) %>%
mutate("variable" = "cyl")
mtcars_gear <- mtcars %>%
group_by(gear) %>%
summarise("sum_weight" = sum(wt)) %>%
mutate("variable" = "gear") %>%
rename(level = gear)
#make summarized data set example
mtcars_sum <- mtcars_wght %>%
bind_rows(mtcars_gear) %>%
mutate("coefficient" = "x", "std.error" = "y", "p_value" = "z")
推荐答案
你是t之后他的输出?
# A tibble: 6 x 6
level sum_weight variable coefficient std.error p_value
<chr> <dbl> <chr> <dbl> <dbl> <dbl>
1 4 25.1 cyl NA NA NA
2 6 21.8 cyl -6.66 1.63 0.000353
3 8 56.0 cyl -10.5 1.96 0.0000109
4 3 58.4 gear NA NA NA
5 4 31.4 gear 1.32 1.93 0.498
6 5 13.2 gear 1.50 1.85 0.426
您可以使用 dplyr
和扫帚
来实现。
df <- rbind(mtcars_wght, mtcars_gear)
df <- df %>% mutate(
level = paste0(variable, level)
) %>% select(-variable)
mod_summary <- model1 %>% broom::tidy()
left_join(df, mod_summary, by = c('level' = 'term')) %>%
mutate(variable = str_extract(level, '[a-z]+'),
level = str_extract(level, '[0-9]+')) %>%
rename(coefficient = estimate, p_value = p.value) %>%
select(level, sum_weight, variable, coefficient, std.error, p_value)
编辑
如果要包含拦截
,请使用 full_join
代替 left_join
以上。在下面,我将输出保存到摘要
。
Edit
If you want to include the Intercept
, use full_join
instead of left_join
above. Below, I saved the output to thesummary
.
thesummary <- full_join(df, mod_summary, by = c('level' = 'term')) %>%
mutate(variable = str_extract(level, '[A-Za-z]+'),
level = str_extract(level, '[0-9]+')) %>%
rename(coefficient = estimate, p_value = p.value) %>%
select(level, sum_weight, variable, coefficient, std.error, p_value)
要分配 1
以获取缺失值,仅对最后4列执行:
To assign 1
for missing values, for the last 4 columns only, do:
cbind(thesummary[,1:2], apply(thesummary[,3:6], 2, function(x) ifelse(is.na(x), 1, x)))
以下是输出:
level sum_weight variable coefficient std.error p_value
1 4 25.14 cyl 1 1 1
2 6 21.82 cyl -6.656 1.629 3.528e-04
3 8 55.99 cyl -10.542 1.958 1.087e-05
4 3 58.39 gear 1 1 1
5 4 31.40 gear 1.324 1.928 4.980e-01
6 5 13.16 gear 1.500 1.855 4.257e-01
7 <NA> NA Intercept 25.428 1.881 1.554e-13
如果要替换每个 NA
与`,只需这样做:
If you want to replace every NA
with `, simply do:
thesummary[is.na(thesummary)] <- 1
这里是输出。
# A tibble: 7 x 6
level sum_weight variable coefficient std.error p_value
<chr> <dbl> <chr> <dbl> <dbl> <dbl>
1 4 25.1 cyl 1.00 1.00 1.00e+ 0
2 6 21.8 cyl -6.66 1.63 3.53e- 4
3 8 56.0 cyl -10.5 1.96 1.09e- 5
4 3 58.4 gear 1.00 1.00 1.00e+ 0
5 4 31.4 gear 1.32 1.93 4.98e- 1
6 5 13.2 gear 1.50 1.85 4.26e- 1
7 1 1.00 Intercept 25.4 1.88 1.55e-13
这篇关于正确将系数绑定到汇总表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!