-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathWeek3_worksheet.R
More file actions
177 lines (115 loc) · 4.87 KB
/
Copy pathWeek3_worksheet.R
File metadata and controls
177 lines (115 loc) · 4.87 KB
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
#### Week 3 Worksheet ####
library(tufte)
library(haven)
library(data.table)
library(JWileymisc)
library(psych)
library(ggplot2)
library(ggpubr)
library(ggthemes)
library(scales)
library(ggExtra)
# Remember to set your working directory to the folder that these datasets are in
setwd("~/git_repos/MonashHonoursStatistics")
db <- as.data.table(read_sav("[2021] PSY4210 BL.sav")) # baseline
dd <- as.data.table(read_sav("[2021] PSY4210 DD.sav")) # daily
#### 1. Scoring and Reliability ####
# Score some other scales in the baseline questionnaire!
#
# Four items from the Sternberger's Trait Anxiety scale (STAI; items STAI1-STAI4)
# were used in to measure anxiety levels. The short-form of the UCLA Loneliness
# Scale (ULS-8; items ULS1-ULS8) was used to capture loneliness.
# The Lifespan Self-Esteem Scale (items LSE1-LSE4) was used to measure self-esteem or how one
# feels about themselves. The 10-item version of the Big Five inventory was used
# to capture openness, conscientiousness, extraversion, agreeableness and
# neuroticism (e.g. BFI_E1r needs to be reverse-coded and then added to or
# averaged with BFI_E2 to form the Extraversion score).
# Calculate Cronbach's alpha measure of internal consistency reliability
# for the self-esteem score (LSE items)
psych::alpha(as.data.frame(db[, .(LSE1, LSE2, LSE3, LSE4)]), check.keys = TRUE)
## Create a variable for self-esteem and name it SE, and others if you like!
db[, SE := rowMeans(.SD, na.rm = TRUE),
.SDcols = c("LSE1", "LSE2", "LSE3", "LSE4")]
#### 2. Basic ggplot2 graphs ####
# Remember how we make a density plot graph for just one variable,
# say, SE from our db dataset (we just created it)
???
# Remember that we have to tell R that certain variables are factors
# Do that now for the variable "relsta" (relationship status).
# Hint: If you aren't sure what the levels and labels are, try str(db$relsta) first
db[, relsta := factor(
relsta, levels = c(1,2,3),
labels = c("single", "in a committed exclusive relationship", "in a committed nonexclusive relationship"))]
# Now plot SE by relationship status (hint: remove NAs from relsta)
ggplot(db[!is.na(relsta)], aes(SE, colour = relsta)) +
geom_density()
## Next, let's try to see how self-esteem varies by Stress categories
## First, do you remember how to reverse scores PSS items?
db [, PSS2 := 6- PSS2r]
db [, PSS3 := 6- PSS3r]
## Do you remember how to create a Stress total?
db[, Stress := rowMeans(.SD, na.rm = TRUE) * 4,
.SDcols = c("PSS1", "PSS2", "PSS3", "PSS4")]
## Finally, remind us how to create high vs. low stress categories (called StrCat)
## (but unlike the reading, do this on Stress total, not StressAVG)
## Hint: what number should you put instead of 3? How did I get 3 when I did
## the code for StressAVG?
mean(db$Stress)
db[Stress < 10, StrCat := "low"]
db[Stress >= 10, StrCat := "high"]
db[, StrCat := factor(StrCat, levels = c("low", "high"))]
## It's always good to check you did a categorization correctly:
table(db$StrCat)
## Now that we have all that, create a density plot showing SE by Stress Category
???
## use `testDistribution()` to examine whether self esteem follows a
## normal distribution and whether it has any extreme values.
plot(testDistribution(db$SE,
extremevalues = "theoretical", ev.perc = .005))
## Make a histogram for the variable: `extraversion`.
???
???
???
## Make a dotplot for: `openness` *separated* (e.g., by colour and/or fill)
## by `relsta`:
???
???
???
#### 3. Bivariate graphs ####
# Make a scatter plot (points) for `extraversion` and
# `conscientiousness` in the baseline data, `db`. Use the
# good visualization principles we have learned. Make your own
# decisions to make the scatter plot most useful to read.
???
???
???
## Check what the correlation between Self-Esteem and Neuroticism is
???
???
cor.test(???)
## Make a scatterplot showing the association between Self-Esteem and Neuroticism
## Include a linear regression line and print the statistics
## Hint 1: neuroticism has some missing values that cause trouble
## Hint 2: play around with the x, y, hjust and vjust arguments in annotate()
cool_plot <- ggplot(???) +
geom_point() +
stat_smooth(???) +
scale_x_continuous(breaks = as.numeric(quantile(db$neuroticism, na.rm = TRUE))) +
scale_y_continuous(breaks = as.numeric(quantile(db$SE))) +
theme_pubr() +
theme(axis.line = element_blank()) +
geom_rangeframe() +
xlab(???) +
ylab(???) +
annotate(???,
label = ???,
size = ???, hjust = ???, vjust = ???)
## Bonus: Include histograms of Self-Esteem and Neuroticism
ggMarginal(???)
## Epic Bonus:
## Now find a way to show the same scatterplot above, but represent
## differences by relsta
## Hint: First find a way to get the stats separately by relsta
cor.test(???)
cor.test(???)
cool_plot2 <- ???