mtcars-simple1

regression
en
bayes
frequentist
qm1
stats-nutshell
mcars
Published

September 4, 2022

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:

lm1_freq <- lm(mpg ~ hp + cyl + disp, data = mtcars)

library(rstanarm)
lm1_bayes <- stan_glm(mpg ~ hp + cyl + disp, data = mtcars, refresh = 0)

Get parameters:

library(easystats)
parameters(lm1_freq)
Parameter   | Coefficient |   SE |         95% CI | t(28) |      p
------------------------------------------------------------------
(Intercept) |       34.18 | 2.59 | [28.88, 39.49] | 13.19 | < .001
hp          |       -0.01 | 0.01 | [-0.04,  0.02] | -1.00 | 0.325 
cyl         |       -1.23 | 0.80 | [-2.86,  0.41] | -1.54 | 0.135 
disp        |       -0.02 | 0.01 | [-0.04,  0.00] | -1.81 | 0.081 
parameters(lm1_bayes)
Parameter   | Median |         95% CI |     pd |  Rhat |     ESS |                   Prior
------------------------------------------------------------------------------------------
(Intercept) |  34.17 | [28.48, 39.44] |   100% | 1.001 | 2712.00 | Normal (20.09 +- 15.07)
hp          |  -0.02 | [-0.05,  0.01] | 85.72% | 1.000 | 2599.00 |   Normal (0.00 +- 0.22)
cyl         |  -1.20 | [-2.80,  0.43] | 93.00% | 1.001 | 2274.00 |   Normal (0.00 +- 8.44)
disp        |  -0.02 | [-0.04,  0.00] | 95.78% | 1.001 | 2442.00 |   Normal (0.00 +- 0.12)

The coefficient is estimated as about -0.01


Categories:

  • regression
  • en
  • bayes
  • frequentist
  • qm1
  • stats-nutshell