library(psych) # for the describe() command
library(naniar) # for the gg_miss-upset() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To select rows from data: rows(mtcars, am==0)
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## To return to the console output, use 'expss_output_default()'.
##
## Attaching package: 'expss'
## The following object is masked from 'package:naniar':
##
## is_na
d2 <- read.csv(file="data/EAMMi2_final.csv", header=T) # import the file you created in last lab
head(d2)
## race_rc gender stress swb efficacy mindful
## 1 white f 3.3 4.333333 3.4 6.6
## 2 white m 3.6 4.166667 3.4 7.2
## 3 white m 3.3 1.833333 2.2 6.8
## 4 other f 3.2 5.166667 2.8 6.8
## 5 white m 3.5 3.666667 3.0 5.8
## 6 white f 2.9 4.000000 2.4 5.6
str(d2)
## 'data.frame': 3182 obs. of 6 variables:
## $ race_rc : chr "white" "white" "white" "other" ...
## $ gender : chr "f" "m" "m" "f" ...
## $ stress : num 3.3 3.6 3.3 3.2 3.5 2.9 3.2 3 2.9 3.2 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ efficacy: num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ mindful : num 6.6 7.2 6.8 6.8 5.8 ...
# use as.factor() command to make sure your categorical variables are recognized as such
d2$race_rc <- as.factor(d2$race_rc)
d2$gender <- as.factor(d2$gender)
describe(d2)
## vars n mean sd median trimmed mad min max range skew kurtosis
## race_rc* 1 3173 5.53 2.13 7.00 5.88 0.00 1 7.00 6.00 -0.98 -0.68
## gender* 2 3178 1.28 0.49 1.00 1.21 0.00 1 3.00 2.00 1.40 0.88
## stress 3 3175 3.27 0.41 3.30 3.26 0.44 1 5.00 4.00 -0.16 2.67
## swb 4 3178 4.47 1.32 4.67 4.53 1.48 1 7.00 6.00 -0.36 -0.46
## efficacy 5 3176 3.13 0.45 3.10 3.13 0.44 1 4.00 3.00 -0.29 0.63
## mindful 6 3173 5.29 0.84 5.27 5.29 0.79 3 7.87 4.87 0.06 -0.13
## se
## race_rc* 0.04
## gender* 0.01
## stress 0.01
## swb 0.02
## efficacy 0.01
## mindful 0.01
# use the hist() command to create a histogram for your continuous variables
hist(d2$swb)
hist(d2$stress)
hist(d2$efficacy)
hist(d2$mindful)
# use the table() command to create a table for your categorical variables (other than your ID variable)
table(d2$race_rc, useNA = "always")
##
## asian black hispanic multiracial nativeamer other
## 210 249 286 293 12 97
## white <NA>
## 2026 9
table(d2$gender, useNA = "always")
##
## f m nb <NA>
## 2332 792 54 4
# use the gg_miss_upset() command to visualize your missing data
gg_miss_upset(d2, nsets = "6")
# create a new dataframe with only your complete cases/observations
d3 <- na.omit(d2)
# use the cross_cases() command to create a crosstab of your categorical variables
cross_cases(d3, race_rc, gender)
|  gender | |||
|---|---|---|---|
|  f |  m |  nb | |
|  race_rc | |||
|    asian | 150 | 57 | 1 |
|    black | 181 | 63 | 2 |
|    hispanic | 206 | 76 | 2 |
|    multiracial | 220 | 61 | 10 |
|    nativeamer | 11 | 1 | |
|    other | 72 | 24 | 1 |
|    white | 1475 | 508 | 37 |
|    #Total cases | 2315 | 790 | 53 |
# use the plot() command to create scatterplots of your continuous variables
plot(d3$mindful, d3$swb,
main="Scatterplot of Mindfulness and Subjective Well-Being",
xlab = "Mindfulness",
ylab = "SWB")
plot(d3$mindful, d3$efficacy,
main="Scatterplot of Mindfulness and Efficacy",
xlab = "Mindfulness",
ylab = "Efficacy")
plot(d3$mindful, d3$stress,
main="Scatterplot of Mindfulness and Stress",
xlab = "Mindfulness",
ylab = "Stress")
# use the boxplot() command to create boxplots of your continuous and categorical variables
boxplot(data=d3, mindful~race_rc,
main="Boxplot of Mindfulness and Participant Race",
xlab = "Participant Race",
ylab = "Mindfulness")
boxplot(data=d3, swb~race_rc,
main="Boxplot of Subjective Well-Being and Participant Race",
xlab = "Participant Race",
ylab = "SWB")
boxplot(data=d3, stress~race_rc,
main="Boxplot of Stress and Participant Race",
xlab = "Participant Race",
ylab = "Stress")
boxplot(data=d3, efficacy~race_rc,
main="Boxplot of Efficacy and Participant Race",
xlab = "Participant Race",
ylab = "Efficacy")
boxplot(data=d3, mindful~gender,
main="Boxplot of Mindfulness and Gender",
xlab = "Gender",
ylab = "Mindfulness")
boxplot(data=d3, swb~gender,
main="Boxplot of Subjective Well-Being and Gender",
xlab = "Gender",
ylab = "SWB")
boxplot(data=d3, stress~gender,
main="Boxplot of Stress and Gender",
xlab = "Gender",
ylab = "Stress")
boxplot(data=d3, efficacy~gender,
main="Boxplot of Efficacy and Gender",
xlab = "Gender",
ylab = "Efficacy")