Weitere Quellen zum Thema
Quellen
Bildnachweise
Weitere Quellen zum Thema
Quellen
Bildnachweise
Daten: https://osf.io/eky4s/
Gamble, T., & Walker, I. (2016). Wearing a bicycle helmet […] Psych Science, 27, 289-294.
Gamble, T., & Walker, I. (2016). Wearing a bicycle helmet […] Psych Science, 27, 289-294.
install.packages(““)
Nach der Installation, muss das Paket geladen werden.
library(paketname)
require(paketname)
Daten aus OSF herunterladen
require(osfr)
## Loading required package: osfr
dir.create("daten")
## Warning in dir.create("daten"): 'daten' existiert bereits
osf_retrieve_node("eky4s") %>% osf_ls_files(pattern="csv") %>% osf_download(conflicts = "overwrite", path = "daten")
## # A tibble: 1 x 4 ## name id local_path meta ## <chr> <chr> <chr> <list> ## 1 GambleWalkerPsychologi… 56448fcf8c5e4a… daten/GambleWalkerPsycholo… <named li…
Daten lokal einlesen
library(readr) dats <- read_delim("daten/GambleWalkerPsychologicalScienceData.csv", "\t", escape_double = FALSE, col_types = cols(Condition = col_factor(levels = c("1", "2"))), trim_ws = TRUE) head(dats)
## # A tibble: 6 x 11 ## ID Condition Age Sex STAI_S_Y_PRE SSS_total BART STAI_S_Y_DURING ## <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 1 1 25 1 37 18 3.67 32 ## 2 2 1 24 2 27 19 36.5 30 ## 3 3 1 26 1 30 35 29.3 34 ## 4 4 1 24 1 30 28 30.5 28 ## 5 5 1 24 2 32 26 24.1 44 ## 6 6 1 24 2 34 12 32.1 30 ## # … with 3 more variables: STAI_S_Y_POST <dbl>, Cycling_Frequency <dbl>, ## # Helmet_Use_Likelihood <dbl>
Anlegen eines Faktors
library(dplyr)
## ## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats': ## ## filter, lag
## The following objects are masked from 'package:base': ## ## intersect, setdiff, setequal, union
dats = dats %>% mutate(Condition=factor(Condition, labels=c('helmet','cap'))) head(dats)
## # A tibble: 6 x 11 ## ID Condition Age Sex STAI_S_Y_PRE SSS_total BART STAI_S_Y_DURING ## <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 1 helmet 25 1 37 18 3.67 32 ## 2 2 helmet 24 2 27 19 36.5 30 ## 3 3 helmet 26 1 30 35 29.3 34 ## 4 4 helmet 24 1 30 28 30.5 28 ## 5 5 helmet 24 2 32 26 24.1 44 ## 6 6 helmet 24 2 34 12 32.1 30 ## # … with 3 more variables: STAI_S_Y_POST <dbl>, Cycling_Frequency <dbl>, ## # Helmet_Use_Likelihood <dbl>
Spalten aus Daten auswählen mit select
subset_dats = dats %>% select(BART,Condition) subset_dats
## # A tibble: 80 x 2 ## BART Condition ## <dbl> <fct> ## 1 3.67 helmet ## 2 36.5 helmet ## 3 29.3 helmet ## 4 30.5 helmet ## 5 24.1 helmet ## 6 32.1 helmet ## 7 50.7 helmet ## 8 26.3 helmet ## 9 41.0 helmet ## 10 20.6 helmet ## # … with 70 more rows
Zeilen aus den Daten basierend auf einem Kriterium in einer Spalte auswählen mit filter
subset_dats_filter = subset_dats %>% filter(BART > 40) subset_dats_filter
## # A tibble: 29 x 2 ## BART Condition ## <dbl> <fct> ## 1 50.7 helmet ## 2 41.0 helmet ## 3 42.5 cap ## 4 41.6 cap ## 5 47.8 cap ## 6 55.5 cap ## 7 48.0 helmet ## 8 43.4 helmet ## 9 58.1 helmet ## 10 61.5 helmet ## # … with 19 more rows
Zeilen sortieren basierend auf Werten in einer Spalte mit arrange
subset_dats_filter_arrange = subset_dats_filter %>% arrange(desc(BART)) subset_dats_filter_arrange
## # A tibble: 29 x 2 ## BART Condition ## <dbl> <fct> ## 1 81.3 helmet ## 2 79.2 helmet ## 3 77.1 helmet ## 4 70.3 helmet ## 5 63.5 helmet ## 6 61.5 helmet ## 7 58.1 helmet ## 8 57.4 helmet ## 9 56.6 cap ## 10 55.7 helmet ## # … with 19 more rows
Alle Schritte in einem Befehl
dats %>% select(BART,Condition) %>% filter(BART > 40) %>% arrange(Condition)
## # A tibble: 29 x 2 ## BART Condition ## <dbl> <fct> ## 1 50.7 helmet ## 2 41.0 helmet ## 3 48.0 helmet ## 4 43.4 helmet ## 5 58.1 helmet ## 6 61.5 helmet ## 7 70.3 helmet ## 8 41.4 helmet ## 9 77.1 helmet ## 10 81.3 helmet ## # … with 19 more rows
One more thing… Der Befehl mutate, um neue Variablen zu erstellen
dats = dats %>% rowwise() %>% mutate(mean_stai=mean(c(STAI_S_Y_PRE,STAI_S_Y_DURING,STAI_S_Y_POST))) dats[1:10,10:12]
## # A tibble: 10 x 3 ## # Rowwise: ## Cycling_Frequency Helmet_Use_Likelihood mean_stai ## <dbl> <dbl> <dbl> ## 1 2 1 33.7 ## 2 2 2 27 ## 3 3 2 30.7 ## 4 4 3 29.3 ## 5 2 1 36 ## 6 5 6 31 ## 7 4 4 28 ## 8 3 1 24 ## 9 5 6 25.7 ## 10 2 1 32.7
dats %>% group_by() %>% summarise(mean = mean(BART), sd = sd(BART), max=max(BART), min = min(BART), n = n(), )
## # A tibble: 1 x 5 ## mean sd max min n ## <dbl> <dbl> <dbl> <dbl> <int> ## 1 35.6 16.4 81.3 2.68 80
dats %>% group_by(Condition) %>% summarise(mean = mean(BART), sd = sd(BART), max=max(BART), min = min(BART), n = n(), )
## # A tibble: 2 x 6 ## Condition mean sd max min n ## <fct> <dbl> <dbl> <dbl> <dbl> <int> ## 1 helmet 40.4 18.2 81.3 3.67 39 ## 2 cap 31.1 13.3 56.6 2.68 41
Varianzhomogenität
library(car)
## Loading required package: carData
## ## Attaching package: 'car'
## The following object is masked from 'package:dplyr': ## ## recode
leveneTest(BART~Condition,data=dats)
## Levene's Test for Homogeneity of Variance (center = median) ## Df F value Pr(>F) ## group 1 1.8755 0.1748 ## 78
library(ggplot2) p <- ggplot(dats, aes(Condition,BART)) p + geom_boxplot()
Normalverteilung
shapiro.test(unlist(dats %>% filter(Condition == "cap") %>% select(BART)))
## ## Shapiro-Wilk normality test ## ## data: unlist(dats %>% filter(Condition == "cap") %>% select(BART)) ## W = 0.98406, p-value = 0.8247
shapiro.test(unlist(dats %>% filter(Condition == "helmet") %>% select(BART)))
## ## Shapiro-Wilk normality test ## ## data: unlist(dats %>% filter(Condition == "helmet") %>% select(BART)) ## W = 0.95333, p-value = 0.1064
qqnorm(unlist(dats %>% filter(Condition == "cap") %>% select(BART))) qqline(unlist(dats %>% filter(Condition == "cap") %>% select(BART)))
qqnorm(unlist(dats %>% filter(Condition == "helmet") %>% select(BART))) qqline(unlist(dats %>% filter(Condition == "helmet") %>% select(BART)))
model = t.test(BART ~ Condition, data = dats, var.equal = T) model
## ## Two Sample t-test ## ## data: BART by Condition ## t = 2.6326, df = 78, p-value = 0.01021 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## 2.276655 16.402669 ## sample estimates: ## mean in group helmet mean in group cap ## 40.40308 31.06341
Effektstärke
library(effsize) cohen.d(BART ~ Condition,data = dats)
## ## Cohen's d ## ## d estimate: 0.5888439 (medium) ## 95 percent confidence interval: ## lower upper ## 0.1339958 1.0436920