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.1945212 | 0.95 | 28.9639907 | 39.4651160 | 1.00000 | 1.001264 | 2497.060 | normal | 20.09062 | 15.0673701 |
| hp | -0.0145072 | 0.95 | -0.0447344 | 0.0158835 | 0.82875 | 1.000293 | 2617.386 | normal | 0.00000 | 0.2197599 |
| cyl | -1.2368250 | 0.95 | -2.9050724 | 0.4344811 | 0.93375 | 1.002053 | 1787.573 | normal | 0.00000 | 8.4367476 |
| disp | -0.0186804 | 0.95 | -0.0392118 | 0.0014009 | 0.96575 | 1.001482 | 1843.708 | normal | 0.00000 | 0.1215712 |
The coefficient is estimated as about -0.01
Categories:
- regression
- en
- bayes
- frequentist
- qm1
- stats-nutshell