# if you haven't used a given package before, you'll need to download it first
# delete the "#" before the install function and run it to download
# re-insert the "#" 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 Data
# Import the "fakedata.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
table(d2$age)
##
## 1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45
## 627 56 6 88
## 5 over 45
## 185
table(d2$mhealth)
##
## anxiety disorder bipolar
## 99 4
## depression eating disorders
## 24 18
## none or NA obsessive compulsive disorder
## 761 17
## other ptsd
## 23 16
# use histograms to visualize continuous data
hist(d2$pas_covid)
hist(d2$pss)
hist(d2$support)
hist(d2$phq)
describe(d2)
## vars n mean sd median trimmed mad min max range skew
## X 1 962 4692.48 2588.09 4820.50 4743.31 3327.70 20 8860 8840 -0.12
## age* 2 962 2.11 1.66 1.00 1.89 0.00 1 5 4 0.95
## mhealth* 3 962 4.62 1.39 5.00 4.88 0.00 1 8 7 -1.51
## pas_covid 4 962 3.23 0.69 3.22 3.24 0.66 1 5 4 -0.22
## pss 5 962 2.90 0.95 2.75 2.89 1.11 1 5 4 0.13
## support 6 962 3.60 0.94 3.67 3.65 0.99 1 5 4 -0.43
## phq 7 962 2.05 0.86 1.89 1.97 0.99 1 4 3 0.67
## kurtosis se
## X -1.22 83.44
## age* -0.94 0.05
## mhealth* 2.69 0.04
## pas_covid 0.10 0.02
## pss -0.75 0.03
## support -0.59 0.03
## phq -0.59 0.03
## 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, age, mhealth)
 mhealth | ||||||||
---|---|---|---|---|---|---|---|---|
 anxiety disorder |  bipolar |  depression |  eating disorders |  none or NA |  obsessive compulsive disorder |  other |  ptsd | |
 age | ||||||||
   1 under 18 | 65 | 2 | 10 | 15 | 495 | 12 | 16 | 12 |
   2 between 18 and 25 | 12 | 1 | 1 | 3 | 35 | 3 | 1 | |
   3 between 26 and 35 | 1 | 5 | ||||||
   4 between 36 and 45 | 13 | 1 | 4 | 65 | 2 | 3 | ||
   5 over 45 | 8 | 9 | 161 | 3 | 4 | |||
   #Total cases | 99 | 4 | 24 | 18 | 761 | 17 | 23 | 16 |
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$pas_covid, d2$support,
main="Pandemic Anxiety Scale and Social Support Measure",
xlab = "Pandemic Anxiety Scale",
ylab = "Social Support Measure")
plot(d2$pss, d2$phq,
main="Scatterplot of Perceived Stress Scale-4 and Patient Health Questionnaire-9",
xlab = "Perceived Stress Scale-4",
ylab = "Patient Health Questionnaire-9")
# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your 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 variable names, not their 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, pas_covid~age,
main="Boxplot of Age and Pandemic Anxiety Scale",
xlab = "Pandemic Anxiety Scale",
ylab = "Age")
boxplot(data=d2, support~mhealth,
main="Boxplot of Mental Health Disorders and Social Support Measure",
xlab = "Social Support Measure",
ylab = "Mental Health Disorders")
# 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 hypotheses. You may repeat 1 variable to see its association with others. Again, 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 variable names, not their scales, so someone reading your plots can understand them.
We did it!!