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?
- Berechnen Sie die Modellkoeffizienten! Tipp:
lm() - Berechnen Sie im Anschluss die Vorhersagen dieses Modells. Tipp:
predict()mitmutate() - Visualisieren Sie dann das Modell Tipp:
ggplot()undgeom_smooth()oder mittels einer anderer Methode. - Berechnen Sie die Residuen:
e = echtem Y-Wert und vorhergesagtem Y-Wert. Tipp:mutate(). - Berechnen Sie die Korrelation zwischen Spritverbrauch und Hubraum! Tipp:
summarise() mitcor()`.
TippLösung
315.1 Vorbereitung
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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)315.2 Ad 1
lm1 <- lm(mpg ~ disp, data = mtcars)
lm1
##
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
##
## Coefficients:
## (Intercept) disp
## 29.59985 -0.04122315.3 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.61283Sondern die Predictions als neue Spalte in mtcars anlegen. Viel sauberer!
315.4 Ad 3
ggplot(mtcars2) +
aes(y = mpg, x = disp) +
geom_point() +
geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'Andere Visualisierung:
Oder so:
library(easystats)
## # Attaching packages: easystats 0.7.5 (red = needs update)
## ✖ bayestestR 0.17.0 ✔ correlation 0.8.8
## ✖ datawizard 1.3.1 ✖ effectsize 1.0.2
## ✖ insight 1.5.1 ✖ modelbased 0.15.0
## ✖ performance 0.16.0 ✖ parameters 0.29.0
## ✔ report 0.6.4 ✖ see 0.13.0
##
## Restart the R-Session and update packages with `easystats::easystats_update()`.
estimate_expectation(lm1, by = "disp") %>% plot()315.5 Ad 4
Der “Absolutwert” kickt das Vorzeichen weg. Das machen wir, wenn wir meinen, dass das Vorzeichen egal ist.
315.6 Bonus-Aufgabe
Berechnen Sie den mittleren Fehler über alle e!
315.7 Ad 5
Aufgabenname: mtcars-regr01
Categories:
- lm
- mtcars
- correlation
- regression
- string