Running linear model (lm) across multiple grouping variables.

grouped_lm(
  data,
  grouping.vars,
  ...,
  output = "tidy",
  tidy.args = list(conf.int = TRUE, conf.level = 0.95),
  augment.args = list()
)

Arguments

data

Dataframe (or tibble) from which variables are to be taken.

grouping.vars

Grouping variables.

...

Additional arguments to broom::tidy, broom::glance, or broom::augment S3 method.

output

A character describing what output is expected. Two possible options: "tidy" (default), which will return the results, or "glance", which will return model summaries.

tidy.args

A list of arguments to be used in the relevant S3 method.

augment.args

A list of arguments to be used in the relevant S3 method.

Value

A tibble dataframe with tidy results from linear model.

See also

Examples

# loading needed libraries library(ggplot2) # getting tidy output of results grouped_lm( data = mtcars, grouping.vars = cyl, formula = mpg ~ am * wt, output = "tidy" )
#> # A tibble: 12 x 9 #> cyl term estimate std.error statistic p.value conf.low conf.high #> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 4 (Intercept) 13.9 16.1 0.865 0.416 -24.1 51.9 #> 2 4 am 30.3 17.2 1.77 0.121 -10.3 70.9 #> 3 4 wt 3.07 5.44 0.564 0.590 -9.79 15.9 #> 4 4 am:wt -11.0 6.16 -1.78 0.118 -25.5 3.61 #> 5 6 (Intercept) 63.6 14.1 4.51 0.0204 18.7 109. #> 6 6 am -41.4 19.0 -2.18 0.117 -102. 19.1 #> 7 6 wt -13.1 4.16 -3.16 0.0511 -26.4 0.113 #> 8 6 am:wt 12.5 6.22 2.02 0.137 -7.26 32.4 #> 9 8 (Intercept) 25.1 3.51 7.14 0.0000315 17.2 32.9 #> 10 8 am -2.92 25.9 -0.113 0.912 -60.5 54.7 #> 11 8 wt -2.44 0.842 -2.90 0.0159 -4.32 -0.563 #> 12 8 am:wt 0.439 7.63 0.0575 0.955 -16.6 17.4 #> significance #> <chr> #> 1 ns #> 2 ns #> 3 ns #> 4 ns #> 5 * #> 6 ns #> 7 ns #> 8 ns #> 9 *** #> 10 ns #> 11 * #> 12 ns
# getting model summaries # diamonds dataset from ggplot2 grouped_lm( data = diamonds, grouping.vars = c(cut, color), formula = price ~ carat * clarity, output = "glance" )
#> # A tibble: 35 x 14 #> cut color r.squared adj.r.squared sigma statistic p.value df logLik #> <ord> <ord> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 Fair D 0.915 0.906 1005. 106. 1.03e- 70 15 -1350. #> 2 Fair E 0.917 0.912 883. 179. 8.70e-106 13 -1830. #> 3 Fair F 0.917 0.912 954. 217. 9.37e-150 15 -2575. #> 4 Fair G 0.932 0.929 964. 273. 5.03e-164 15 -2595. #> 5 Fair H 0.932 0.929 1033. 332. 1.76e-161 12 -2526. #> 6 Fair I 0.958 0.955 794. 307. 1.14e-104 12 -1410. #> 7 Fair J 0.955 0.950 907. 204. 1.82e- 66 11 -973. #> 8 Good D 0.933 0.931 831. 600. 0. 15 -5382. #> 9 Good E 0.927 0.926 905. 781. 0. 15 -7667. #> 10 Good F 0.915 0.914 939. 644. 0. 15 -7504. #> AIC BIC deviance df.residual nobs #> <dbl> <dbl> <dbl> <int> <int> #> 1 2733. 2786. 148463951. 147 163 #> 2 3690. 3741. 163677866. 210 224 #> 3 5184. 5248. 269389292. 296 312 #> 4 5224. 5288. 277025077. 298 314 #> 5 5081. 5133. 309753202. 290 303 #> 6 2848. 2892. 102049583. 162 175 #> 7 1972. 2008. 88046170. 107 119 #> 8 10797. 10874. 446239835. 646 662 #> 9 15369. 15451. 750592238. 917 933 #> 10 15042. 15123. 787519475. 893 909 #> # ... with 25 more rows