forked from psych251/problem_sets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
215 lines (215 loc) · 8.19 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
library(foreign) # for reading spss formatted data
library(tidyr)
library(dplyr)
library(stringr) # useful for some string manipulation
library(ggplot2)
d = read.spss("data/Tamiretal2008ReplicationData.sav", to.data.frame=T)
head(d)
colnames(d)
## your code here
likert <- select(d,`d$Game1Angry1`)
## your code here
likert <- select(d,'Game1Angry1')
## your code here
likert <- select(d,'Game1Angry1')
range(likert)
## your code here
likert <- select(d,'Game1Angry1')
head(likert)
## your code here
likert <- select(d,'Game1Angry1')
hist(likert)
## your code here
likert <- select(d,'Game1Angry1').
## your code here
likert <- select(d,'Game1Angry1')
view(likert)
## your code here
d$Game1Angry1 %>%
unique
## your code here
d$Game1Angry1 %>%
range
## your code here
d$Game1Angry1 %>%
unique
## your code here
d$Game1Angry1 %>%
unique %>%
mean
## your code here
d$Game1Angry1 %>%
unique
## your code here
d$Game1Angry1 %>%
unique
tail(d)
head(d&DoNotUse)
d&DoNotUse
d$DoNotUse
filtered_d = d %>%
filter( ) # your code here: exclude subjects that are marked as "DoNotUse"
d$DoNotUse
filtered_d = d %>%
filter(?unique %>% ?is.na) # your code here: exclude subjects that are marked as "DoNotUse"
d$DoNotUse
filtered_d = d %>%
filter(unique %>% is.na) # your code here: exclude subjects that are marked as "DoNotUse"
d$DoNotUse
filtered_d = d %>%
filter(is.na(?unique)) # your code here: exclude subjects that are marked as "DoNotUse"
d$DoNotUse
filtered_d = d %>%
filter(is.na(?unique))
filtered_d$DoNotUse
# your code here: exclude subjects that are marked as "DoNotUse"
d$DoNotUse
filtered_d = d %>%
filter(is.na(?unique))
filtered_d
# your code here: exclude subjects that are marked as "DoNotUse"
d$DoNotUse
filtered_d = d %>%
filter(is.na(?unique))
head(filtered_d)
# your code here: exclude subjects that are marked as "DoNotUse"
filtered_d = d %>%
filter(is.na(?unique))
filtered_d$DoNotUse
# your code here: exclude subjects that are marked as "DoNotUse"
filtered_d = d %>%
filter(is.na(DoNotUse))
filtered_d$DoNotUse
# your code here: exclude subjects that are marked as "DoNotUse"
filtered_d = d %>%
filter(is.na(DoNotUse))
tail(filtered_d)
# your code here: exclude subjects that are marked as "DoNotUse"
filtered_d = d %>%
filter(is.na(DoNotUse))
head(filtered_d)
# your code here: exclude subjects that are marked as "DoNotUse"
filtered_d = d %>%
filter(?is.na(DoNotUse))
filtered_d = d %>%
filter(!is.na(DoNotUse))
head(filtered_d)
# your code here: exclude subjects that are marked as "DoNotUse"
filtered_d = d %>%
filter(is.na(DoNotUse))
head(filtered_d)
# your code here: exclude subjects that are marked as "DoNotUse"
filtered_d = filtered_d %>%
select(c("Subject", "Cond"), # Generally important columns for both hypotheses
contains("Game"), # we want all the game columns for hypothesis 1
-contains("Intro"), -c("WhichGames", "GameComments"), # except these
starts_with("DinerDashWith"), c("SOFMusicEnemies", "SOFNoMusicEnemies")) # These columns are for hypothesis 2
rating_hyp_d = filtered_d %>%
filter(is.na(DoNotUseVideoGamePerformanceData)) %>% # first, let's get rid of the subjects who did so poorly on one game that their data is unusable
select(-DoNotUseVideoGamePerformanceData, # now get rid of that column
-starts_with("DinerDash"), # and the other columns we don't need
-starts_with("SOF"))
performance_hyp_d = filtered_d %>%
select(contains("Game")) # your code here: remove the columns containing "Game" in the name
tiny_demo_d = head(performance_hyp_d, 2) # get just the first two subjects performance data, for a demo
tiny_demo_d
tiny_demo_d %>% pivot_longer(cols=-c("Subject", "Cond"), # this tells it to transform all columns *except* these ones
names_to='Measurement',
values_to='Value')
performance_hyp_d = filtered_d %>%
select(-contains("Game")) # your code here: remove the columns containing "Game" in the name
tiny_demo_d = head(performance_hyp_d, 2) # get just the first two subjects performance data, for a demo
tiny_demo_d
tiny_demo_d %>% pivot_longer(cols=-c("Subject", "Cond"), # this tells it to transform all columns *except* these ones
names_to='Measurement',
values_to='Value')
performance_hyp_long_d = performance_hyp_d %>%
pivot_longer(cols=-c("Subject", "Cond"),
names_to='Measurement',
values_to='Score')
head(performance_hyp_long_d)
rating_hyp_long_d = rating_hyp_d %>%
## your code here
pivot_longer(cols=-c("Key", "Value"),
names_to='Measurement',
values_to='Rating')
rating_hyp_long_d = rating_hyp_d %>%
## your code here
pivot_longer(cols=-c("Key", "Value"),
names_to='Measurement',
values_to='Rating')
head(rating_hyp_long_d)
head(rating_hyp_d)
rating_hyp_long_d = rating_hyp_d %>%
## your code here
pivot_longer(cols=-c("Key", "Value"),
names_to='Measurement',
values_to='Rating')
head(rating_hyp_d)
#head(rating_hyp_long_d)
head(rating_hyp_d)
#head(rating_hyp_long_d)
rating_hyp_long_d = rating_hyp_d %>%
pivot_longer(cols=-c("Subject", "Cond"),
names_to='Measurement',
values_to='Rating')
head(rating_hyp_long_d)
performance_hyp_long_d = performance_hyp_long_d %>%
mutate(ConfrontationalGame = grepl("SOF", Measurement), # create a new variable that will say whether the measurement was of the game soldier of fortune (SOF).
WithMusic = !grepl("NoMusic|WithoutMusic", Measurement), # creates a new column named WithMusic, which is False if the measurement contains *either* "NoMusic" or "WithoutMusic"
MusicCondition = factor(ifelse(Cond > 3, Cond-3, Cond), levels = 1:3, labels = c("Anger", "Exciting", "Neutral"))) # Get rid of uninterpretable condition labels
rating_hyp_long_d = rating_hyp_long_d %>%
mutate(
IsRecall = grepl("Friends|Strangers", Measurement),## Your code here
)
rating_hyp_long_d = rating_hyp_long_d %>%
mutate(
IsRecall = grepl("Friends|Strangers", Measurement),## Your code here
)
head(rating_hyp_long_d)
rating_hyp_long_d = rating_hyp_long_d %>%
mutate(
IsRecall = grepl("Friends|Strangers", Measurement),## Your code here
)
#head(rating_hyp_long_d)
rating_hyp_long_d = rating_hyp_long_d %>%
mutate(
GameNumber = as.numeric(substr(rating_hyp_long_d$Measurement, 5, 5)),
ConfrontationalGame = GameNumber <= 2, # in a mutate, we can use a column we created (or changed) right away. Games 1 and 2 are confrontational, games 3 and 4 are not.
Emotion = str_extract(Measurement, "Angry|Neutral|Excited|Exciting|Calm"),
Emotion = ifelse(Emotion == "Excited", "Exciting", # this just gets rid of some annoying labeling choices
ifelse(Emotion == "Calm", "Neutral", Emotion))
)
performance_hyp_long_d %>%
group_by(ConfrontationalGame) %>%
summarize(AvgScore = mean(Score, na.rm=T)) # the na.rm tells R to ignore NA values
performance_hyp_long_d = performance_hyp_long_d %>%
group_by(ConfrontationalGame, WithMusic) %>% # we're going to compute four sets of z-scores, one for the confrontational game without music, one for the confrontational game with, one for the nonconfrontational game without music, and one for the nonconfrontational game with
mutate(z_scored_performance = scale(Score)) %>%
ungroup()
rating_summary_d = rating_hyp_long_d %>%
group_by(ConfrontationalGame,Emotion) %>%
summarize(MeanRating = mean(Rating, na.rm=T))
## your code here
rating_summary_d
ggplot(rating_summary_d, aes(x=ConfrontationalGame, y=MeanRating, fill=Emotion)) +
geom_bar(position="dodge", stat="identity") +
scale_fill_brewer(palette="Set1")
model = lm(Rating ~ ConfrontationalGame * Emotion, rating_hyp_long_d)
summary(model)
performance_diff_d = performance_hyp_long_d %>%
mutate(WithMusic = factor(WithMusic, levels=c(F, T), labels=c("PreMusic", "PostMusic"))) %>% # first, tweak the variable so our code is easier to read.
select(-c("Score", "Measurement")) %>% # now we remove columns we don't need (bonus: leave them in and see if you can understand what goes wrong!)
spread(WithMusic, z_scored_performance) %>%
mutate(ImprovementScore=PostMusic-PreMusic)
performance_diff_d
performance_diff_summary_d = performance_diff_d %>%
group_by(ConfrontationalGame,MusicCondition) %>%
summarize(MeanImprovementScore = mean(ImprovementScore, na.rm=T))
## Your code here
performance_diff_summary_d
ggplot(performance_diff_summary_d, aes(x=ConfrontationalGame, y=MeanImprovementScore, fill=MusicCondition)) +
geom_bar(position="dodge", stat="identity") +
scale_fill_brewer(palette="Set1")
performance_model = lm(ImprovementScore ~ ConfrontationalGame * MusicCondition, performance_diff_d)
summary(performance_model)