![](/img/trans.png)
[英]Get `chisq.test()$p.value` for several groups using `dplyr::group_by()`
[英]Regression estimates by group with p.value stars
我使用以下命令按組運行多個 lm 回歸
by_country <- group_by(df, country)
print(do(by_country,
tidy(lm(y ~ x*z, data=.))), n=500)
我添加了 print 語句只是為了查看完整的表格。 現在想把output改成顯示星星來顯示顯着程度。
如果我將代碼更改為
df %>%
do(by_country,
tidy(lm(y ~ x*z, data=.))) %>%
mutate(signif = stars.pval(p.value))
我得到錯誤:錯誤:只能提供一個未命名的參數,而不是 2。
如何將 lm 和 mutate 組合在一起? 還是有其他更方便的方法?
我正在尋找以下/類似的 output:
|Country|term|estimate|std.error|stars
|:---- |:------:| -----:|-----:|-----:|
| UK | x:z | ... | ... | ***|
do
已被取代。 此代碼似乎適用於默認mtcars
數據。
library(dplyr)
df <- mtcars
df %>%
group_by(cyl) %>%
summarise(model = list(broom::tidy(lm(disp ~ mpg*am, data = cur_data())))) %>%
tidyr::unnest(model) %>%
mutate(signif = gtools::stars.pval(p.value))
# cyl term estimate std.error statistic p.value signif
# <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
# 1 4 (Intercept) -69.4 129. -0.537 0.608 " "
# 2 4 mpg 8.96 5.64 1.59 0.156 " "
# 3 4 am 270. 132. 2.04 0.0806 "."
# 4 4 mpg:am -12.8 5.73 -2.23 0.0609 "."
# 5 6 (Intercept) -147. 225. -0.653 0.561 " "
# 6 6 mpg 18.4 11.7 1.56 0.216 " "
# 7 6 am 64.7 682. 0.0949 0.930 " "
# 8 6 mpg:am -6.84 33.4 -0.205 0.851 " "
# 9 8 (Intercept) 566. 106. 5.36 0.000320 "***"
#10 8 mpg -13.9 6.91 -2.00 0.0729 "."
#11 8 am -1203. 1736. -0.693 0.504 " "
#12 8 mpg:am 76.4 113. 0.678 0.513 " "
示例數據:
set.seed(111)
df = data.frame(country = sample(c("A","B"),100,replace=TRUE),
x=runif(100),y=runif(100),z=runif(100))
您不需要傳遞 data.frame:
by_country <- group_by(df, country)
do(by_country,
tidy(lm(y ~ x*z, data=.))) %>%
mutate(signif = stars.pval(p.value))
或者:
df %>%
group_by(country) %>%
do(tidy(lm(y ~ x*z, data=.))) %>%
mutate(signif = stars.pval(p.value))
兩者都給出:
# A tibble: 8 x 7
# Groups: country [2]
country term estimate std.error statistic p.value signif
<fct> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
1 A (Intercept) 0.808 0.154 5.25 0.00000324 "***"
2 A x -0.552 0.307 -1.80 0.0780 "."
3 A z -0.491 0.326 -1.50 0.139 " "
4 A x:z 0.853 0.552 1.55 0.129 " "
5 B (Intercept) 0.0658 0.194 0.339 0.736 " "
6 B x 0.503 0.300 1.68 0.101 " "
7 B z 0.866 0.400 2.17 0.0360 "*"
8 B x:z -0.857 0.609 -1.41 0.167 " "
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.