Inhaltsverzeichnis
Bootstrapping in R
meine_funktion <- function(datensatz, i){
mean(datensatz$variable[i], na.rm=T)
}
mein_objekt <- boot(datensatz, meine_funktion, 2000)
boot.ci(mein_objekt, type="bca")
Bootstrapping Schritt-für-Schritt am Beispiel Mittelwert
library(boot)
library(tibble)
library(yarrr)
as_tibble(pirates)
# A tibble: 1,000 x 17
id sex age height weight headband college tattoos tchests parrots favorite.pirate sword.type
<int> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
1 1 male 28 173. 70.5 yes JSSFP 9 0 0 Jack Sparrow cutlass
2 2 male 31 209. 106. yes JSSFP 9 11 0 Jack Sparrow cutlass
3 3 male 26 170. 77.1 yes CCCC 10 10 1 Jack Sparrow cutlass
4 4 fema~ 31 144. 58.5 no JSSFP 2 0 2 Jack Sparrow scimitar
5 5 fema~ 41 158. 58.4 yes JSSFP 9 6 4 Hook cutlass
6 6 male 26 190. 85.4 yes CCCC 7 19 0 Jack Sparrow cutlass
7 7 fema~ 31 158. 59.6 yes JSSFP 9 1 7 Blackbeard cutlass
8 8 fema~ 31 173. 74.5 yes JSSFP 5 13 7 Hook cutlass
9 9 fema~ 28 165. 68.7 yes JSSFP 12 37 2 Anicetus cutlass
10 10 male 30 184. 84.7 yes JSSFP 12 69 4 Jack Sparrow cutlass
# ... with 990 more rows, and 5 more variables: eyepatch <dbl>, sword.time <dbl>, beard.length <dbl>,
# fav.pixar <chr>, grogg <dbl>
Funktion für Bootstrapping
mi_fun_alter <- function(pirates, i){
mean(pirates$age[i], na.rm=TRUE)
}
Erstellen des Bootstrap-Objektes
mi_obj_alter <- boot(pirates, mi_fun_alter, 2000)
mi_obj_alter
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = pirates, statistic = mi_fun_alter, R = 2000)
Bootstrap Statistics :
original bias std. error
t1* 27.36 0.0022285 0.179735
Erstellen des Konfidenzintervalls
boot.ci(mi_obj_alter, type="bca")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 2000 bootstrap replicates
CALL :
boot.ci(boot.out = mi_obj_alter, type = "bca")
Intervals :
Level BCa
95% (26.98, 27.68 )
Calculations and Intervals on Original Scale
boot.ci(mi_obj_alter, type="bca", conf=.9)
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 2000 bootstrap replicates
CALL :
boot.ci(boot.out = mi_obj_alter, conf = 0.9, type = "bca")
Intervals :
Level BCa
90% (27.04, 27.63 )
Calculations and Intervals on Original Scale
Bootstrapping am Beispiel Median
Funktion für Bootstrapping
me_fun_alter <- function(pirates, i){
median(pirates$age[i], na.rm=TRUE)
}
Erstellen des Bootstrap-Objekts
me_obj_alter <- boot(pirates, me_fun_alter, 2000)
me_obj_alter
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = pirates, statistic = me_fun_alter, R = 2000)
Bootstrap Statistics :
original bias std. error
t1* 27 0.182 0.380721
Erstellen des Konfidenzintervalls
boot.ci(me_obj_alter, type="bca", conf=.98)
Fehler in bca.ci(boot.out, conf, index[1L], L = L, t = t.o, t0 = t0.o, :
geschätzte Anpassung 'w' ist unendlich
Bootstrapping am Beispiel Standardabweichung
sd_fun_alter <- function(pirates, i){
sd(pirates$age[i], na.rm=TRUE)
}
sd_obj_alter <- boot(pirates, sd_fun_alter, 2000)
sd_obj_alter
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = pirates, statistic = sd_fun_alter, R = 2000)
Bootstrap Statistics :
original bias std. error
t1* 5.786526 -0.00133567 0.132637
boot.ci(sd_obj_alter, type="bca")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 2000 bootstrap replicates
CALL :
boot.ci(boot.out = sd_obj_alter, type = "bca")
Intervals :
Level BCa
95% ( 5.543, 6.059 )
Calculations and Intervals on Original Scale
Bootstrapping am Beispiel Korrelationskoeffizient
Funktion für Bootstrapping
r_fun_schaetze_papagei <- function(pirates, i){
cor(pirates$tchests[i], pirates$parrots[i], use="pairwise.complete.obs")
}
Erstellen des Bootstrap-Objektes
r_obj_schaetze_papagei <- boot(pirates, r_fun_schaetze_papagei, 2000)
r_obj_schaetze_papagei
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = pirates, statistic = r_fun_schaetze_papagei, R = 2000)
Bootstrap Statistics :
original bias std. error
t1* 0.09077387 0.001348948 0.03858567
Erstellen des Konfidenzintervalls
boot.ci(r_obj_schaetze_papagei, type="bca")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 2000 bootstrap replicates
CALL :
boot.ci(boot.out = r_obj_schaetze_papagei, type = "bca")
Intervals :
Level BCa
95% ( 0.0203, 0.1744 )
Calculations and Intervals on Original Scale
Bootstrapping am Beispiel Determinationskoeffizient
Funktion für Bootstrapping
r2_fun_schaetze_alter <- function(pirates, i){
summary(lm(pirates$tchests[i] ~ pirates$age[i]))$r.squared
}
attributes(summary(lm(pirates$tchests ~ pirates$age)))
$names
[1] "call" "terms" "residuals" "coefficients" "aliased" "sigma"
[7] "df" "r.squared" "adj.r.squared" "fstatistic" "cov.unscaled"
$class
[1] "summary.lm"
Erstellen des Bootstrap-Objekts
r2_obj_schaetze_alter <- boot(pirates, r2_fun_schaetze_alter, 2000)
r2_obj_schaetze_alter
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = pirates, statistic = r2_fun_schaetze_alter, R = 2000)
Bootstrap Statistics :
original bias std. error
t1* 0.03778454 0.0007115778 0.01032019
Erstellen des Konfidenzintervalls
boot.ci(r2_obj_schaetze_alter, type="bca")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 2000 bootstrap replicates
CALL :
boot.ci(boot.out = r2_obj_schaetze_alter, type = "bca")
Intervals :
Level BCa
95% ( 0.0201, 0.0597 )
Calculations and Intervals on Original Scale
Konfidenzintervall für Regressionskoeffizienten
Funktion für Bootstrapping
b_fun_schaetze_alter <- function(pirates, i){
summary(lm(pirates$tchests[i] ~ pirates$age[i]))$coefficients[2,1]
}
Erstellen des Bootstrap-Objektes
b_obj_schaetze_alter <- boot(pirates, b_fun_schaetze_alter, 2000)
b_obj_schaetze_alter
Erstellen des Konfidenzintervalls
boot.ci(b_obj_schaetze_alter, type="bca")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 2000 bootstrap replicates
CALL :
boot.ci(boot.out = b_obj_schaetze_alter, type = "bca")
Intervals :
Level BCa
95% ( 0.5764, 1.0838 )
Calculations and Intervals on Original Scale
Verwendete Pakete und Funktionen
base
Funktion | Beschreibung |
---|---|
attributes(summary(lm())) | Verfügbare Variablennamen der Ergebniszusammenfassung abfragen. |
function(Liste der Argumente) | Macht die Definitionen von neuen eigenen Funktionen in R möglich. |
mean(Variable, na.rm=T) | Mittelwert bestimmen. – Missings ausschließen. |
summary(lm())$coefficients[2,1] | Regressionskoeffizient abfragen. |
summary(lm())$r.squared | Determinationskoeffizient für lineare Regression abfragen. |
bootstrap
Funktion | Beschreibung |
---|---|
boot( Datensatz, Funktion, Anzahl Bootstrap-Stichproben) | Bootstrap-Objekt erzeugen. |
boot.ci(Bootstrap-Objekt, type=”bca”) | Bootstrap-Konfidenzintervall ermitteln. – Es stehen unterschiedliche Typen zur Verfügung: norm, basic, stud, perc, bca – Relevant für M2 ist “bca” und steht für “bias corrected accelerated” |
stats
Funktion | Beschreibung |
---|---|
cor( Variable1, Variable2, use=”pairwise.complete.obs”) | Produkt-Moment-Korrelationskoeffizient berechnen: – Paarweise gültige Fälle einbeziehen. |
lm(Kriteriumsvariable ~ Prädiktorvariable) | Einfache lineare Regression durchführen. |
median(Variable) | Median berechnen. |
sd(Variable) | Standardabweichung berechnen. 👉 Es handelt sich dabei um die Populationsstandardabweichung mit dem Nenner (n-1). |
tibble
Funktion | Beschreibung |
---|---|
as_tibble() | Eine Datensatz, wie bspw. einen Data Frame, eine List oder Matrix in ein sogenanntes Tibble umwandeln. Ein Tibble ist ein Data Frame der Klasse tbl_df. |