Zum Hauptinhalt springen

347  tidymodels-simple1

Schlüsselwörter

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

Schreiben Sie einen maximal einfachen Code für ein lineares Modell mit tidymodels.

Hinweise:

  • Datensatz: mtcars
  • Modellformel: mpg ~ hp
  • Verzichten Sie auf Resampling jeder Art.

Berichten Sie das adjustierte R-Quadrat!

347.1 Setup

library(tidymodels)
## ── Attaching packages ────────────────────────────────────── tidymodels 1.5.0 ──
## ✔ broom        1.0.13     ✔ recipes      1.3.3 
## ✔ dials        1.4.3      ✔ rsample      1.3.2 
## ✔ dplyr        1.2.1      ✔ tailor       0.1.0 
## ✔ ggplot2      4.0.3      ✔ tidyr        1.3.2 
## ✔ infer        1.1.0      ✔ tune         2.1.0 
## ✔ modeldata    1.5.1      ✔ workflows    1.3.0 
## ✔ parsnip      1.6.0      ✔ workflowsets 1.1.1 
## ✔ purrr        1.2.2      ✔ yardstick    1.4.0
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ purrr::discard() masks scales::discard()
## ✖ dplyr::filter()  masks stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
## ✖ recipes::step()  masks stats::step()
data(mtcars)

347.2 Fitten

m1 <-
  fit(linear_reg(), mpg ~ hp, data = mtcars)
m1  %>% tidy()
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)  30.1       1.63       18.4  6.64e-18
## 2 hp           -0.0682    0.0101     -6.74 1.79e- 7
m1 %>% glance()
## # A tibble: 1 × 12
##   r.squared adj.r.squared sigma statistic     p.value    df logLik   AIC   BIC
##       <dbl>         <dbl> <dbl>     <dbl>       <dbl> <dbl>  <dbl> <dbl> <dbl>
## 1     0.602         0.589  3.86      45.5 0.000000179     1  -87.6  181.  186.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>

Lösung: 0.59.

Aufgabenname: tidymodels-simple1