# 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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
##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$gender)
##
## female I use another term male Prefer not to say
## 1010 33 196 19
table(d2$mhealth)
##
## anxiety disorder bipolar
## 130 6
## depression eating disorders
## 32 30
## none or NA obsessive compulsive disorder
## 970 26
## other ptsd
## 37 27
# use histograms to visualize continuous data
hist(d2$rse)
hist(d2$pss)
hist(d2$phq)
hist(d2$gad)
describe(d2)
## vars n mean sd median trimmed mad min max range skew
## X 1 1258 4699.13 2586.15 4835.50 4752.27 3341.04 1 8867 8866 -0.14
## gender* 2 1258 1.38 0.80 1.00 1.21 0.00 1 4 3 1.74
## mhealth* 3 1258 4.64 1.42 5.00 4.87 0.00 1 8 7 -1.36
## rse 4 1258 2.63 0.72 2.70 2.65 0.74 1 4 3 -0.21
## pss 5 1258 2.93 0.95 3.00 2.92 1.11 1 5 4 0.10
## phq 6 1258 2.07 0.86 1.89 1.99 0.99 1 4 3 0.64
## gad 7 1258 2.04 0.91 1.71 1.95 0.85 1 4 3 0.68
## kurtosis se
## X -1.22 72.91
## gender* 1.39 0.02
## mhealth* 2.43 0.04
## rse -0.71 0.02
## pss -0.74 0.03
## phq -0.69 0.02
## gad -0.72 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 (5) were within the accepted range (-2/+2). However, (1) variable (mhealth) was outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.
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 | 7 | 1 | 1 | 16 | 5 | 3 | ||
   Prefer not to say | 2 | 2 | 14 | 1 | ||||
   female | 99 | 3 | 29 | 28 | 782 | 22 | 26 | 21 |
   male | 22 | 2 | 2 | 158 | 4 | 5 | 3 | |
   #Total cases | 130 | 6 | 32 | 30 | 970 | 26 | 37 | 27 |
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$phq, d2$pss,
main="Scatterplot of Patient Health Questionaire9 and Perceived Stress",
xlab = "phq",
ylab = "pss")
plot(d2$phq, d2$rse,
main="Scatterplot of Patient Health Questionaire9 and Self-Esteem",
xlab = "phq",
ylab = "rse")
# 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, gad~gender,
main="Boxplot of Gender Identity and Generalized Anxiety Disorder7 Scores",
xlab = "gender",
ylab = "gad")
boxplot(data=d2, phq~rse,
main="Boxplot of Rosenberg Self Esteem Inventory Score and Patient Health Questionaire9",
xlab = "rse",
ylab = "phq")
# 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!!