# if you haven't used a given package before, you'll need to download it first
# after download is finished, insert a "#" before the install function so that the file will Knit later
# then run the library function calling that package
#install.packages("psych")
#install.packages("expss")
library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To get total summary skip 'by' argument: take_all(mtcars, mean)
# Import the "fakedata_2025.csv" file
d2 <- read.csv("Data/projectdata.csv")
str(d2)
## 'data.frame': 716 obs. of 7 variables:
## $ X : int 520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
## $ gender : chr "female" "male" "female" "male" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "none or NA" ...
## $ covid_pos : int 0 0 0 0 0 0 0 0 0 0 ...
## $ covid_neg : int 0 0 0 0 0 0 0 0 0 0 ...
## $ isolation_c: num 1 1 1 1 1 1 1 1 1 1 ...
## $ support : num 2.83 3 4 4 3.67 ...
# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab
Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.
# use tables to visualize categorical data (2 variables)
table(d2$gender)
##
## female I use another term male Prefer not to say
## 562 20 124 10
table(d2$mhealth)
##
## anxiety disorder bipolar
## 79 3
## depression eating disorders
## 13 20
## none or NA obsessive compulsive disorder
## 554 15
## other ptsd
## 19 13
# use histograms to visualize continuous data (4 variables)
hist(d2$covid_pos)
hist(d2$covid_neg)
hist(d2$isolation_c)
hist(d2$support)
describe(d2)
## vars n mean sd median trimmed mad min max range
## X 1 716 5198.56 2588.76 5783.50 5347.72 3020.06 20 8860.0 8840.0
## gender* 2 716 1.42 0.82 1.00 1.25 0.00 1 4.0 3.0
## mhealth* 3 716 4.61 1.43 5.00 4.85 0.00 1 8.0 7.0
## covid_pos 4 716 2.48 3.60 0.00 1.80 0.00 0 15.0 15.0
## covid_neg 5 716 1.41 1.92 0.00 1.09 0.00 0 8.0 8.0
## isolation_c 6 716 2.29 0.81 2.25 2.30 1.11 1 3.5 2.5
## support 7 716 3.48 0.95 3.50 3.51 0.99 1 5.0 4.0
## skew kurtosis se
## X -0.42 -1.12 96.75
## gender* 1.59 0.83 0.03
## mhealth* -1.43 2.36 0.05
## covid_pos 1.29 0.60 0.13
## covid_neg 1.04 -0.14 0.07
## isolation_c -0.03 -1.23 0.03
## support -0.30 -0.65 0.04
## For the required write-up below, choose one of these options to paste and edit below based on your output.
## OPTION 1
# We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).
## OPTION 2
# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name(s) here) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.
We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).
Crosstabs are used to visualize combinations of two categorical variables.
cross_cases(d2, gender, mhealth)
|  mhealth | ||||||||
|---|---|---|---|---|---|---|---|---|
|  anxiety disorder |  bipolar |  depression |  eating disorders |  none or NA |  obsessive compulsive disorder |  other |  ptsd | |
|  gender | ||||||||
|    I use another term | 5 | 1 | 1 | 8 | 4 | 1 | ||
|    Prefer not to say | 1 | 1 | 7 | 1 | ||||
|    female | 58 | 1 | 10 | 19 | 437 | 15 | 13 | 9 |
|    male | 15 | 1 | 2 | 102 | 1 | 3 | ||
|    #Total cases | 79 | 3 | 13 | 20 | 554 | 15 | 19 | 13 |
## Some students may have issues with this function working. If this happens to you, please try these 2 options:
## Option 1: install the "maditr" package and then call in its library.
## Option 2: If Option 1 doesn't work, then you will use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line.
# xtabs(~ + , data=)
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$covid_neg, d2$support,
main="Scatterplot of Negative Effects of COVID-19 and Social Support",
xlab = "Negative Effects of COVID-19",
ylab = "Social Support")
plot(d2$covid_neg, d2$isolation_c,
main="Scatterplot of Negative Effects of COVID-19 and Isolation",
xlab = "Negative Effects of COVID-19",
ylab = "Isolation")
# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your potential hypotheses. You may repeat 1 variable to see its association with 2 others. You will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.
Boxplots are used to visualize combinations of one categorical and one continuous variable.
# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable'
boxplot(data=d2, covid_neg~mhealth,
main="Boxplot of Negative Effects of COVID-19 by Mental Health",
xlab = "Mental Health",
ylab = "Negative Effects of COVID-19")
boxplot(data=d2, covid_pos~mhealth,
main="Boxplot of Positive Effects of COVID-19 by Mental Health",
xlab = "Positive Effects of COVID-19",
ylab = "Mental Health")
# Note: for HW, you will choose to plot 2 combos of any of your 4 continuous variables with either of your 2 categorical variables, based on your potential hypotheses. You may repeat 1 variable to see its association with others. Again, will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.
That’s it!!