# 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 select columns from data: columns(mtcars, mpg, vs:carb)
##
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
## To return to the console output, use 'expss_output_default()'.
# Import the "fakedata_2025.csv" file
d2 <- read.csv("Data/projectdata.csv")
# 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$relationship_status)
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 49
## Prefer not to say
## 29
## Single, divorced or widowed
## 1
## Single, never married
## 300
table(d2$mhealth)
##
## anxiety disorder bipolar
## 52 3
## depression eating disorders
## 9 19
## none or NA obsessive compulsive disorder
## 251 15
## other ptsd
## 19 12
# use histograms to visualize continuous data (4 variables)
hist(d2$big5_neu)
hist(d2$edeq12)
hist(d2$brs)
hist(d2$isolation_c)
describe(d2)
## vars n mean sd median trimmed mad min max
## X 1 380 7528.38 728.60 7481.50 7518.40 914.76 6291 8858.0
## relationship_status* 2 380 4.45 1.10 5.00 4.69 0.00 1 5.0
## mhealth* 3 380 4.57 1.66 5.00 4.69 0.00 1 8.0
## big5_neu 4 380 5.07 1.37 5.33 5.22 1.48 1 7.0
## edeq12 5 380 2.16 0.81 2.08 2.13 1.05 1 4.0
## brs 6 380 2.63 0.87 2.67 2.62 0.99 1 5.0
## isolation_c 7 380 2.63 0.74 2.75 2.69 0.74 1 3.5
## range skew kurtosis se
## X 2567.0 0.12 -1.16 37.38
## relationship_status* 4.0 -1.58 0.70 0.06
## mhealth* 7.0 -0.94 0.83 0.09
## big5_neu 6.0 -0.85 0.25 0.07
## edeq12 3.0 0.23 -1.09 0.04
## brs 4.0 0.15 -0.59 0.04
## isolation_c 2.5 -0.48 -0.94 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, relationship_status, mhealth)
|  mhealth | ||||||||
|---|---|---|---|---|---|---|---|---|
|  anxiety disorder |  bipolar |  depression |  eating disorders |  none or NA |  obsessive compulsive disorder |  other |  ptsd | |
|  relationship_status | ||||||||
|    In a relationship/married and cohabiting | 1 | |||||||
|    In a relationship/married but living apart | 11 | 1 | 3 | 28 | 3 | 1 | 2 | |
|    Prefer not to say | 7 | 2 | 14 | 2 | 4 | |||
|    Single, divorced or widowed | 1 | |||||||
|    Single, never married | 34 | 2 | 9 | 14 | 208 | 9 | 14 | 10 |
|    #Total cases | 52 | 3 | 9 | 19 | 251 | 15 | 19 | 12 |
## 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$edeq12, d2$big5_neu,
main="Scatterplot of Symptoms of Disordered Eating and Neuroticism",
xlab = "Symptoms of Disordered Eating",
ylab = "Neuroticism")
plot(d2$big5_neu, d2$brs,
main="Scatterplot of Neuroticism and Resilience",
xlab = "Neuroticism",
ylab = "Resilience")
# 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, brs~relationship_status,
main="Boxplot of Resilience by Relationship Status",
xlab = "Relationship Status",
ylab = "Resilience")
boxplot(data=d2, edeq12~relationship_status,
main="Boxplot of Symptoms of Disordered Eating by Relationship Status",
xlab = "Relationship Status",
ylab = "Symptoms of Disordered Eating")
# 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!!