Dies ist ein Tutorial zum Selbststudium von Johannes Gerwien.
Dieses kurze Manual soll dabei helfen, Daten auf einfache Weise zu erkunden (“Datenexploration”). Es setzt voraus, dass Nutzer bereits eine ungefähre Vorstellung von der Arbeit mit R bzw. R Studio haben. Ein R manual für Anfänger finden Sie hier.
Es handelt sich bei diesem Manual eigentlich um nichts weiter als eine Zusammenstellung von dem Code, den ich in fast allen meinen Projekten verwende. Dabei liegt der Fokus vor allem auf Einfachheit.
Wir beschäftigen uns mit zwei Ansätzen:
Insbesondere die Datenexploration mit Grafiken hilft fast immer dabei, die Daten in einem ersten Schritt zu “verstehen” und/oder Fehler bei der Kodierung zu erkennen. Außerdem sind die Mittel für die Datenexploration, die ich hier zeige, so einfach, dass sie sich auch eignen, um Zwischenergebnisse zu betrachten - also während die Datenerhebung noch läuft.
requiredPackages <- c("ggplot2",
"janitor",
"Hmisc",
"plyr",
"scales")
install_pack <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
install_pack(requiredPackages)
## ggplot2 janitor Hmisc plyr scales
## TRUE TRUE TRUE TRUE TRUE
data <- read.csv("test_set3.csv", stringsAsFactors = TRUE)
str(data)
## 'data.frame': 1582 obs. of 7 variables:
## $ subject : int 1 1 1 1 1 1 1 1 1 1 ...
## $ condition1 : Factor w/ 2 levels "AA","AI": 2 2 2 2 1 2 2 1 1 1 ...
## $ condition2 : Factor w/ 2 levels "agent","patient": 1 1 1 1 1 1 1 1 1 1 ...
## $ item : int 38 9 31 4 1 23 25 32 22 15 ...
## $ trial_ID : int 36 27 9 46 35 6 75 76 61 63 ...
## $ dep_V_numeric : int 1520 1360 1673 1383 2140 1848 1730 1925 1053 1967 ...
## $ dep_var_binary: int 1 1 1 1 1 1 1 1 1 1 ...
library(janitor)
tabyl(data, dep_var_binary)%>%
adorn_percentages("col") %>%
adorn_pct_formatting(digits = 1)
## dep_var_binary n percent valid_percent
## 0 0.4 44.2% 45.2%
## 1 0.5 53.5% 54.8%
## NA 0.0 2.2% -
tabyl(data, dep_var_binary)
## dep_var_binary n percent valid_percent
## 0 700 0.44247788 0.4524887
## 1 847 0.53539823 0.5475113
## NA 35 0.02212389 NA
tabyl(data, dep_var_binary, condition1)%>%
adorn_percentages("col") %>%
adorn_pct_formatting(digits = 1)
## dep_var_binary AA AI
## 0 42.0% 46.5%
## 1 55.4% 51.7%
## NA 2.7% 1.8%
tabyl(data, dep_var_binary, condition2)%>%
adorn_percentages("col") %>%
adorn_pct_formatting(digits = 1)
## dep_var_binary agent patient
## 0 7.2% 81.3%
## 1 90.0% 17.1%
## NA 2.8% 1.6%
tabyl(data, dep_var_binary, condition1, condition2)%>%
adorn_percentages("col") %>%
adorn_pct_formatting(digits = 1)
## $agent
## dep_var_binary AA AI
## 0 9.6% 4.8%
## 1 86.9% 93.1%
## NA 3.5% 2.0%
##
## $patient
## dep_var_binary AA AI
## 0 74.6% 87.9%
## 1 23.6% 10.6%
## NA 1.8% 1.5%
der Mittelwert gibt den Durschnitt über mehrere Messungen an
Man kann für sehr viele Dinge den Durchschnitt berechnen, zum Beispiel:
Der Mittelwert selbst ist aber nicht besonders aussagekräftig, wenn man nicht auch die Varianz kennt, also den Bereich, in dem alle oder die meisten Messwerte liegen
wir berechnen hier als Maß für die Varianz zusätzlich zum Mittelwert die Standardabweichung
Unterschied zwischen Standardabweichung (SD) und Standardfehler (SE): hier auf youtube
wir benutzen hier ddply aus dem Paket plyr; man lädt plyr ganz normal mit library(plyr)
mehr zu Zusammenfassungen mit “ddply” cookbook for R
library(plyr)
ddply(data, .(), summarize,
Mittelwert = mean(dep_V_numeric, na.rm=T),
Standardabweichung = sd(dep_V_numeric, na.rm = T))
## .id Mittelwert Standardabweichung
## 1 <NA> 1970.614 783.0747
ddply(data, .(condition1), summarize,
Mittelwert = mean(dep_V_numeric, na.rm=T),
Standardabweichung = sd(dep_V_numeric, na.rm = T))
## condition1 Mittelwert Standardabweichung
## 1 AA 1991.589 784.4395
## 2 AI 1949.638 781.6404
ddply(data, .(condition1, condition2), summarize,
Mittelwert = mean(dep_V_numeric, na.rm=T),
Standardabweichung = sd(dep_V_numeric, na.rm = T))
## condition1 condition2 Mittelwert Standardabweichung
## 1 AA agent 1977.879 811.6258
## 2 AA patient 2005.404 756.8406
## 3 AI agent 1909.297 760.8597
## 4 AI patient 1989.675 800.6807
by_sub <- ddply(data, .(subject, condition1, condition2), summarize,
Mittelwert = mean(dep_V_numeric, na.rm=T),
Standardabweichung = sd(dep_V_numeric, na.rm = T))
head(by_sub, 10)
## subject condition1 condition2 Mittelwert Standardabweichung
## 1 1 AA agent 1764.4 478.9545
## 2 1 AA patient 1566.8 521.1839
## 3 1 AI agent 1598.7 248.6055
## 4 1 AI patient 1804.1 453.0127
## 5 2 AA agent 1656.0 263.5130
## 6 2 AA patient 1923.7 421.5316
## 7 2 AI agent 1825.7 235.4136
## 8 2 AI patient 1666.7 274.4433
## 9 3 AA agent 1443.4 351.1325
## 10 3 AA patient 1656.2 286.5057
data$dep_var_binary <- factor(data$dep_var_binary, levels = c("1","0"))
library(ggplot2)
ggplot(data=data, aes(x= dep_var_binary, group=condition1)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop.., accuracy = 0.1),
y= ..prop.. ), stat= "count", vjust = -.5, size = 2.5) +
labs(y = "percent", fill="AV") +
facet_grid(~ condition1) +
scale_y_continuous(labels = scales::percent)+
scale_x_discrete("Kategorien der AV (dep_var_binary)")+
theme_bw()+
ggtitle("Titel der Grafik")
## Warning: The dot-dot notation (`..prop..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(prop)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
ggplot(data=data, aes(x= dep_var_binary, group=condition2)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop.., accuracy = 0.1),
y= ..prop.. ), stat= "count", vjust = -.5, size = 2.5) +
labs(y = "percent", fill="AV") +
facet_grid(~ condition2) +
scale_y_continuous(labels = scales::percent)+
scale_x_discrete("Kategorien der AV (dep_var_binary)", labels = c("yes","no","not available"))+
scale_fill_brewer(palette="Paired")+
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
ggplot(data=subset(data,!is.na(dep_var_binary)), aes(x= dep_var_binary, group=condition1)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop.., accuracy = 0.1),
y= ..prop.. ), stat= "count", vjust = -.5, size = 2.5) +
labs(y = "percent", fill="AV") +
facet_grid(~ condition1 + condition2) +
scale_y_continuous(labels = scales::percent)+
scale_x_discrete("Kategorien der AV (dep_var_binary)")+
scale_fill_brewer(palette="Accent")+
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
ggplot(data, aes(x=condition1, y = dep_V_numeric, color = condition1)) +
stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "pointrange") +
scale_color_brewer(palette="Set1")+
labs(y = "gemittelte Reaktionszeit", x="Bedingung (hier: condition1)") +
theme_bw()+
ggtitle("Titel der Grafik")
ggplot(data, aes(x=condition1, y = dep_V_numeric, group = condition2, color = condition1)) +
stat_summary(fun.data ="mean_cl_boot", geom = "pointrange") +
scale_color_brewer(palette="Set1")+
labs(y = "gemittelte Reaktionszeit", x="Bedingung (hier condition2)") +
facet_wrap(~condition2)+
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
ggplot(data, aes(x=trial_ID, y = dep_V_numeric)) +
stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "errorbar", alpha = 0.5)+
stat_summary(fun ="mean", geom = "line", color = "#8A2BE2", size = 1.5) +
stat_summary(fun ="mean", geom = "point", color = "black", size = 1.5) +
scale_color_brewer(palette="Paired")+
labs(y = "gemittelte Reaktionszeit", x="Trial ID (1-80)") +
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
ggplot(data, aes(x=trial_ID, y = dep_V_numeric, color = condition1)) +
stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "errorbar", alpha = 0.3)+
stat_summary(fun ="mean", geom = "line") +
scale_color_brewer(palette="Set1")+
labs(y = "gemittelte Reaktionszeit", x="Trial ID (1-80)") +
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
ggplot(data, aes(x=trial_ID, y = dep_V_numeric, color = condition1)) +
stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "errorbar", alpha = 0.5)+
stat_summary(fun ="mean", geom = "line") +
scale_color_brewer(palette="Set1")+
facet_wrap(~condition1)+
labs(y = "gemittelte Reaktionszeit", x="Trial ID (1-80)") +
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
ggplot(data, aes(x=trial_ID, y = dep_V_numeric, color = condition1)) +
stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "errorbar", alpha = 0.5)+
stat_summary(fun ="mean", geom = "line") +
scale_color_brewer(palette="Accent")+
facet_wrap(~condition1+condition2)+
labs(y = "gemittelte Reaktionszeit", x="Trial ID (1-80)") +
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
ggplot(data, aes(x=trial_ID, y = dep_V_numeric, color = condition1)) +
stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "errorbar", alpha = 0.5)+
stat_summary(fun ="mean", geom = "line") +
scale_color_brewer(palette="Accent")+
facet_wrap(~condition1+condition2, ncol = 1)+
labs(y = "gemittelte Reaktionszeit", x="Trial ID (1-80)") +
theme_bw()+
theme(legend.position = "none") +
ggtitle("Titel der Grafik")
ggplot(data, aes(x=trial_ID, y = dep_V_numeric, color = condition2)) +
stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "errorbar", alpha = 0.5)+
stat_summary(fun ="mean", geom = "line") +
scale_color_brewer(palette="Accent")+
facet_wrap(~condition1+condition2, ncol = 4)+
labs(y = "gemittelte Reaktionszeit", x="Trial ID (1-80)") +
theme_bw()+
ggtitle("Titel der Grafik")
ggplot(data, aes(x=trial_ID, y = dep_V_numeric, color = condition2)) +
#stat_summary(fun.data ="mean_sdl",fun.args = list(mult = 1), geom = "errorbar", alpha = 0.5) +
stat_summary(fun ="mean", geom = "line") +
scale_color_brewer(palette="Dark2")+
facet_wrap(~condition1+condition2, ncol = 2)+
labs(y = "gemittelte Reaktionszeit", x="Trial ID (1-80)") +
theme_bw()+
ggtitle("Titel der Grafik")+
theme(legend.title=element_blank())
ggsave(filename = "./plots/Dateiname.jpg", last_plot(), width =8, height =4, dpi =300)