Erstellen Sie eine Analyse, die einem typischen Vorhersageprojekt entspricht!
Nutzen Sie den Datensatz penguins!
Sagen Sie die Variable body_mass_g vorher.
Hinweise:
- Halten Sie die Analyse einfach.
- Teilen Sie Test- vs. Train-Set hälftig auf.
- Teilen Sie Analysis vs. Assessment-Set 3:1 auf.
- Den Datensatz
penguinskönnen Sie entweder aus dem Paketpalmerpenguinsbeziehen oder z.B. von hier viaread_csv()importieren. - Orientieren Sie sich im Übrigen an den allgemeinen Hinweisen des Datenwerks.
Pakete laden:
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()
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ lubridate 1.9.5 ✔ tibble 3.3.1
## ✔ readr 2.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ readr::col_factor() masks scales::col_factor()
## ✖ purrr::discard() masks scales::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ stringr::fixed() masks recipes::fixed()
## ✖ dplyr::lag() masks stats::lag()
## ✖ readr::spec() masks yardstick::spec()
## ℹ 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()`.
data("penguins", package = "palmerpenguins")Man erinnere sich, dass ein R-Paket erst (einmalig) installiert sein muss, bevor Sie darauf zugreifen können, etwa um Daten - wie den Datensatz penguins - daraus zu beziehen.
Zeilen mischen und Train- vs. Testset aufteilen:
Das Trainset weiter aufteilen:
d_split <- initial_split(d_train)
d_analysis <- training(d_split)
d_assessment <- testing(d_split)Rezept definieren:
Rezept prüfen:
d_analysis_baked <-
rec1 %>%
prep() %>%
bake(new_data = NULL)
describe_distribution(d_analysis_baked)
## Variable | Mean | SD | IQR | Range
## ---------------------------------------------------------------------
## bill_length_mm | 2.50e-16 | 1.00 | 1.64 | [-2.14, 2.28]
## bill_depth_mm | -4.43e-16 | 1.00 | 1.70 | [-1.98, 2.05]
## flipper_length_mm | -2.99e-17 | 1.00 | 1.62 | [-1.98, 2.10]
## year | 1.31e-14 | 1.00 | 2.47 | [-1.29, 1.18]
## body_mass_g | 4176.37 | 811.07 | 1137.50 | [2850.00, 6300.00]
##
## Variable | Skewness | Kurtosis | n | n_Missing
## ---------------------------------------------------------
## bill_length_mm | 0.11 | -1.05 | 129 | 0
## bill_depth_mm | -0.20 | -0.98 | 129 | 0
## flipper_length_mm | 0.39 | -0.95 | 129 | 0
## year | -0.09 | -1.46 | 129 | 0
## body_mass_g | 0.58 | -0.48 | 128 | 1Workflow und CV definieren:
Fitten (hier kein Tuning):
fit1 <-
wf1 %>%
tune_grid(resamples = cv_scheme)
## Warning: No tuning parameters have been detected, performance will be evaluated using
## the resamples with no tuning.
## Did you want to assign any parameters with a value of `tune()`?Finalisieren:
show_best(fit1)
## Warning in show_best(fit1): No value of `metric` was given; "rmse" will be
## used.
## # A tibble: 1 × 6
## .metric .estimator mean n std_err .config
## <chr> <chr> <dbl> <int> <dbl> <chr>
## 1 rmse standard 334. 2 4.12 pre0_mod0_post0
wf1_final <-
wf1 %>%
finalize_workflow(show_best(fit1))
## Warning in show_best(fit1): No value of `metric` was given; "rmse" will be
## used.
wf1_final
## ══ Workflow ════════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: linear_reg()
##
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 2 Recipe Steps
##
## • step_impute_knn()
## • step_normalize()
##
## ── Model ───────────────────────────────────────────────────────────────────────
## Linear Regression Model Specification (regression)
##
## Computational engine: lmModellgüte:
fit1_final <-
wf1_final %>%
last_fit(d_split)
collect_metrics(fit1_final)
## # A tibble: 2 × 4
## .metric .estimator .estimate .config
## <chr> <chr> <dbl> <chr>
## 1 rmse standard 351. pre0_mod0_post0
## 2 rsq standard 0.795 pre0_mod0_post0
fit1_train <-
wf1_final %>%
fit(d_train)
fit1_test <-
fit1_train %>%
predict(d_test)
head(fit1_test)
## # A tibble: 6 × 1
## .pred
## <dbl>
## 1 3383.
## 2 3621.
## 3 4017.
## 4 3359.
## 5 3384.
## 6 3219.Vgl https://workflows.tidymodels.org/reference/predict-workflow.html
Submitten:
Und als CSV-Datei speichern:
#write_csv(subm_df, file = "submission_blabla.csv")Aufgabenname: predictioncontest1
Categories:
- R
- ds1
- sose22
- string