Description Usage Arguments Examples
This function is meant to be run after 'sm_regression' and will calculate bootstraped model estimates.
1 2 |
x |
'sm_regression' object |
n |
number of bootstrap models to run. Default is 200. |
seed |
a single value, interpreted as an integer. See |
... |
further arguments passed to or from other methods. |
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 52 53 54 55 56 57 58 59 | sm_regression(
data = mtcars,
method = "lm",
formula = mpg ~ am ,
weighting_var = "hp",
lambda = 1,
newdata = data.frame(hp = c(150, 200))
) %>%
add_ci(n = 50)
# example plotting slope coefficient with confidence intervals
## Not run:
library(ggplot2)
mtcars %>%
# calculate locally-weighted regression models
sm_regression(
method = "lm",
formula = mpg ~ am ,
weighting_var = "hp",
newdata = data.frame(hp = seq(125, 175, by = 5))
) %>%
# add models from bootstrap resamples
add_ci(n = 200, seed = 23948) %>%
# calculating confidence interval for beta coefficient
dplyr::mutate(
# extracting central estimate of beta
.coef = purrr::map_dbl(
.model,
~ .x %>% coef() %>% purrr::pluck("am")
),
# extracting each estimate of beta from bootstrapped models
.coef.boot = purrr::map(
.model.boot,
~purrr::map_dbl(
.x,
~ .x %>% coef() %>% purrr::pluck("am")
)
),
# calculating the SD of the beta distribution
.coef.sd = purrr::map_dbl(
.coef.boot,
sd,
na.rm = TRUE
),
# calculating the confidence interval for beta coef
.coef.ll = .coef - qnorm(0.975) * .coef.sd,
.coef.ul = .coef + qnorm(0.975) * .coef.sd,
) %>%
ggplot(aes(x = hp, y = .coef)) +
geom_line() +
geom_ribbon(
aes(ymin = .coef.ll, ymax = .coef.ul),
alpha = 0.4
) +
labs(
y = "Slope coefficient for 'am' when regressed on 'mpg'"
)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.