mtcars-regr01

lm
mtcars
association
regression
string
Published

May 8, 2023

Aufgabe

data("mtcars")

Betrachten Sie folgendes Modell (Datensatz mtcars):

mpg ~ disp

Anders gesagt: Wie gut kann man den Spritverbrauch vorhersagen auf Basis des Hubraums eines Autos?

  1. Berechnen Sie die Modellkoeffizienten! Tipp: lm()
  2. Berechnen Sie im Anschluss die Vorhersagen dieses Modells. Tipp: predict() mit mutate()
  3. Visualisieren Sie dann das Modell Tipp: ggplot() und geom_smooth() oder mittels einer anderer Methode.
  4. Berechnen Sie die Residuen: e = echtem Y-Wert und vorhergesagtem Y-Wert. Tipp: mutate().
  5. Berechnen Sie die Korrelation zwischen Spritverbrauch und Hubraum! Tipp: summarise() mitcor()`.











Lösung

Vorbereitung

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(mtcars)

Ad 1

lm1 <- lm(mpg ~ disp, data = mtcars)
lm1

Call:
lm(formula = mpg ~ disp, data = mtcars)

Coefficients:
(Intercept)         disp  
   29.59985     -0.04122  

Ad 2

Nicht einfach nur predicten:

predict(lm1)
          Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
           23.00544            23.00544            25.14862            18.96635 
  Hornet Sportabout             Valiant          Duster 360           Merc 240D 
           14.76241            20.32645            14.76241            23.55360 
           Merc 230            Merc 280           Merc 280C          Merc 450SE 
           23.79677            22.69220            22.69220            18.23272 
         Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
           18.23272            18.23272            10.14632            10.64090 
  Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
           11.46520            26.35622            26.47987            26.66946 
      Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
           24.64992            16.49345            17.07046            15.17456 
   Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
           13.11381            26.34386            24.64168            25.68030 
     Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
           15.13335            23.62366            17.19410            24.61283 

Sondern die Predictions als neue Spalte in mtcars anlegen. Viel sauberer!

mtcars2  <- 
  mtcars %>% 
  mutate(preds_lm1 = predict(lm1))

Ad 3

ggplot(mtcars2) +
  aes(y = mpg, x = disp) +
  geom_point() +
  geom_smooth(method = "lm")
`geom_smooth()` using formula = 'y ~ x'

Andere Visualisierung:

mtcars2 %>% 
  ggplot(aes(x = disp, y = preds_lm1)) +
  geom_point(size = 2, alpha = .8, color = "pink") +
  geom_line() +
  labs(y = "Vorhergesagte MPG-Werte",
       title = "Vorhersage-Modell lm1")

Oder so:

library(easystats)
# Attaching packages: easystats 0.7.0 (red = needs update)
✔ bayestestR  0.13.1   ✔ correlation 0.8.4 
✖ datawizard  0.9.0    ✔ effectsize  0.8.6 
✔ insight     0.19.7   ✔ modelbased  0.8.6 
✔ performance 0.10.8   ✔ parameters  0.21.3
✔ report      0.5.8    ✔ see         0.8.1 

Restart the R-Session and update packages with `easystats::easystats_update()`.
estimate_expectation(lm1) %>% plot()

Ad 4

mtcars2 <- 
  mtcars2 %>% 
  mutate(e = abs(mpg - preds_lm1))  # abs steht für "Absolutwert"

Der “Absolutwert” kickt das Vorzeichen weg. Das machen wir, wenn wir meinen, dass das Vorzeichen egal ist.

Bonus-Aufgabe

Berechnen Sie den mittleren Fehler über alle e!

mtcars2 %>% 
  summarise(e_avg = mean(e))
     e_avg
1 2.605473

Ad 5

mtcars %>% 
  summarise(cor_mpg_disp = cor(mpg, disp))
  cor_mpg_disp
1   -0.8475514

Categories:

  • lm
  • mtcars
  • correlation
  • regression
  • string