Zum Hauptinhalt springen

351  lm-mario1

Schlüsselwörter

Aufgaben, Statistik, Prognose, Modellierung, R, Datenanalyse, Regression

Sagen Sie den Verkaufspreis vorher für ein Spiel mit 2 Lenkrädern!

Hinweise:

  • Entfernen Sie Spiele mit einem Verkaufspreis von über 100 Euro aus dem Datensatz.

351.0.1 Setup

mariokart <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/openintro/mariokart.csv")

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
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()`.

351.0.2 Bild der Daten

library(ggpubr)  # einmalig vorab installieren, nicht vergessen
## 
## Attache Paket: 'ggpubr'
## Die folgenden Objekte sind maskiert von 'package:datawizard':
## 
##     mean_sd, median_mad
ggscatter(mariokart, x = "wheels", y = "total_pr")  # aus ggpubr

plot of chunk unnamed-chunk-2

plot of chunk unnamed-chunk-2

Oder mit DataExplorer visualisieren:

library(DataExplorer)

plot_scatterplot(mariokart, by = "total_pr")  # "by" ist Y-Achse

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3

351.0.3 Extremwerte entfernen

mariokart2 <- 
  mariokart %>% 
  filter(total_pr < 100)  # alle Spiele teuerer als 100€ entfernen

ggscatter(mariokart2, x = "wheels", y = "total_pr")

plot of chunk unnamed-chunk-4

plot of chunk unnamed-chunk-4

351.0.4 Regressionsgerade eintragen in das Diagramm

ggscatter(mariokart2, 
          x = "wheels", 
          y = "total_pr",
          add = "reg.line")  # Dieser Schalter malt die Regr.gerade in das Diagramm

plot of chunk unnamed-chunk-5

plot of chunk unnamed-chunk-5

351.0.5 Regressionsgerade berechnen

lm_mariokart <- lm(total_pr ~ wheels, data = mariokart2)
lm_mariokart
## 
## Call:
## lm(formula = total_pr ~ wheels, data = mariokart2)
## 
## Coefficients:
## (Intercept)       wheels  
##      37.502        8.643

“lm” wie llineares Modell, also eine Gerade.

351.0.6 Vorhersagen

Vorhersagen funktionieren mit dem Befehl predict.

neues_spiel <-
  data.frame(
    wheels = 2
  )

neues_spiel
##   wheels
## 1      2
predict(lm_mariokart, neues_spiel)  # predicte mir den Verkaufspreis
##        1 
## 54.78743

Aufgabenname: lm-mario1


Categories:

  • R
  • lm
  • predict
  • num