I run multiple lm regressions by group, using the following command
by_country <- group_by(df, country)
print(do(by_country,
tidy(lm(y ~ x*z, data=.))), n=500)
I added the print statement just to see the full table. Now I would like to change the output to display stars to show the level of significance.
If I change the code to
df %>%
do(by_country,
tidy(lm(y ~ x*z, data=.))) %>%
mutate(signif = stars.pval(p.value))
I get the error: Error: Can only supply one unnamed argument, not 2.
How can I combine the group by lm and the mutate? Or is there an alternative, more convenient, way?
I am looking for the following/similar output:
|Country|term|estimate|std.error|stars
|:---- |:------:| -----:|-----:|-----:|
| UK | x:z | ... | ... | ***|
do
has been superseded. This code seems to work fine with default mtcars
data.
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 " "
Example data:
set.seed(111)
df = data.frame(country = sample(c("A","B"),100,replace=TRUE),
x=runif(100),y=runif(100),z=runif(100))
You don't need to pass the data.frame:
by_country <- group_by(df, country)
do(by_country,
tidy(lm(y ~ x*z, data=.))) %>%
mutate(signif = stars.pval(p.value))
Or:
df %>%
group_by(country) %>%
do(tidy(lm(y ~ x*z, data=.))) %>%
mutate(signif = stars.pval(p.value))
Both gives:
# 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 " "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.