Description Usage Arguments Details Value Examples

This function extracts coefficients (and standard error and p-values) of fitted model objects from (nested) data frames, which are saved in a list-variable, and spreads the coefficients into new colummns.

1 2 | ```
spread_coef(data, model.column, model.term, se, p.val, append = TRUE,
...)
``` |

`data` |
A (nested) data frame with a list-variable that contains fitted model objects (see 'Details'). |

`model.column` |
Name or index of the list-variable that contains the fitted model objects. |

`model.term` |
Optional, name of a model term. If specified, only this model term (including p-value) will be extracted from each model and added as new column. |

`se` |
Logical, if |

`p.val` |
Logical, if |

`append` |
Logical, if |

`...` |
Other arguments passed down to the |

This function requires a (nested) data frame (e.g. created by the
`nest`

-function of the tidyr-package),
where several fitted models are saved in a list-variable (see
'Examples'). Since nested data frames with fitted models stored as list-variable
are typically fit with an identical formula, all models have the same
dependent and independent variables and only differ in their
subsets of data. The function then extracts all coefficients from
each model and saves each estimate in a new column. The result
is a data frame, where each *row* is a model with each
model's coefficients in an own *column*.

A data frame with columns for each coefficient of the models
that are stored in the list-variable of `data`

; or, if
`model.term`

is given, a data frame with the term's estimate.
If `se = TRUE`

or `p.val = TRUE`

, the returned data frame
also contains columns for the coefficients' standard error and
p-value.
If `append = TRUE`

, the columns are appended to `data`

,
i.e. `data`

is also returned.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | ```
library(dplyr)
library(tidyr)
library(purrr)
data(efc)
# create nested data frame, grouped by dependency (e42dep)
# and fit linear model for each group. These models are
# stored in the list variable "models".
model.data <- efc %>%
filter(!is.na(e42dep)) %>%
group_by(e42dep) %>%
nest() %>%
mutate(
models = map(data, ~lm(neg_c_7 ~ c12hour + c172code, data = .x))
)
# spread coefficients, so we can easily access and compare the
# coefficients over all models. arguments `se` and `p.val` default
# to `FALSE`, when `model.term` is not specified
spread_coef(model.data, models)
spread_coef(model.data, models, se = TRUE)
# select only specific model term. `se` and `p.val` default to `TRUE`
spread_coef(model.data, models, c12hour)
# spread_coef can be used directly within a pipe-chain
efc %>%
filter(!is.na(e42dep)) %>%
group_by(e42dep) %>%
nest() %>%
mutate(
models = map(data, ~lm(neg_c_7 ~ c12hour + c172code, data = .x))
) %>%
spread_coef(models)
# spread_coef() makes it easy to generate bootstrapped
# confidence intervals, using the 'bootstrap()' and 'boot_ci()'
# functions from the 'sjstats' package, which creates nested
# data frames of bootstrap replicates
library(sjstats)
efc %>%
# generate bootstrap replicates
bootstrap(100) %>%
# apply lm to all bootstrapped data sets
mutate(
models = map(strap, ~lm(neg_c_7 ~ e42dep + c161sex + c172code, data = .x))
) %>%
# spread model coefficient for all 100 models
spread_coef(models, se = FALSE, p.val = FALSE) %>%
# compute the CI for all bootstrapped model coefficients
boot_ci(e42dep, c161sex, c172code)
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.