lm1_freq <- lm(mpg ~ hp + cyl + disp, data = mtcars)
library(rstanarm)
lm1_bayes <- stan_glm(mpg ~ hp + cyl + disp, data = mtcars, refresh = 0)mtcars-simple1
regression
en
bayes
frequentist
qm1
stats-nutshell
mtcars
Exercise
We will use the dataset mtcars in this exercise.
Assume your causal model of your research dictates that fuel economy is a linear function of horse power, cylinder count and displacement of the engine.
Compute the causal effect of horse power given the above model! Report the point estimate.
Notes:
- Use can either use frequentist or bayesian modeling.
- Use R for all computations.
- There are multiple ways to find a solution.
Solution
Compute Model:
Get parameters:
library(easystats)parameters(lm1_freq)| Parameter | Coefficient | SE | CI | CI_low | CI_high | t | df_error | p |
|---|---|---|---|---|---|---|---|---|
| (Intercept) | 34.1849192 | 2.5907776 | 0.95 | 28.8779519 | 39.4918865 | 13.194849 | 28 | 0.0000000 |
| hp | -0.0146793 | 0.0146509 | 0.95 | -0.0446903 | 0.0153316 | -1.001943 | 28 | 0.3249519 |
| cyl | -1.2274199 | 0.7972763 | 0.95 | -2.8605664 | 0.4057265 | -1.539516 | 28 | 0.1349044 |
| disp | -0.0188381 | 0.0104037 | 0.95 | -0.0401491 | 0.0024729 | -1.810711 | 28 | 0.0809290 |
parameters(lm1_bayes)| Parameter | Median | CI | CI_low | CI_high | pd | Rhat | ESS | Prior_Distribution | Prior_Location | Prior_Scale |
|---|---|---|---|---|---|---|---|---|---|---|
| (Intercept) | 34.1526019 | 0.95 | 29.1665749 | 39.4871490 | 1.00000 | 1.000625 | 2482.315 | normal | 20.09062 | 15.0673701 |
| hp | -0.0148549 | 0.95 | -0.0433732 | 0.0144011 | 0.84525 | 1.001873 | 2210.091 | normal | 0.00000 | 0.2197599 |
| cyl | -1.2031823 | 0.95 | -2.8360994 | 0.3379204 | 0.94350 | 1.000715 | 2052.775 | normal | 0.00000 | 8.4367476 |
| disp | -0.0189130 | 0.95 | -0.0396522 | 0.0023981 | 0.95900 | 1.001329 | 2310.101 | normal | 0.00000 | 0.1215712 |
The coefficient is estimated as about -0.01
Categories:
- regression
- en
- bayes
- frequentist
- qm1
- stats-nutshell