# 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)
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## To return to the console output, use 'expss_output_default()'.
##Import Data
d2 <- read.csv("Data/projectdata.csv")
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
## 993 31 194 20
table(d2$mhealth)
##
## anxiety disorder bipolar
## 125 6
## depression eating disorders
## 28 29
## none or NA obsessive compulsive disorder
## 961 27
## other ptsd
## 37 25
# use histograms to visualize continuous data
hist(d2$mfq_26)
hist(d2$support)
hist(d2$gad)
hist(d2$big5_ext)
describe(d2)
## vars n mean sd median trimmed mad min max range
## X 1 1238 4700.94 2594.80 4777.00 4752.62 3392.19 1.0 8867 8866.0
## gender* 2 1238 1.39 0.81 1.00 1.21 0.00 1.0 4 3.0
## mhealth* 3 1238 4.66 1.41 5.00 4.89 0.00 1.0 8 7.0
## mfq_26 4 1238 4.31 0.67 4.35 4.33 0.67 1.8 6 4.2
## support 5 1238 3.57 0.94 3.67 3.62 0.99 1.0 5 4.0
## gad 6 1238 2.04 0.91 1.71 1.95 0.85 1.0 4 3.0
## big5_ext 7 1238 4.34 1.44 4.33 4.38 1.48 1.0 7 6.0
## skew kurtosis se
## X -0.13 -1.23 73.75
## gender* 1.74 1.37 0.02
## mhealth* -1.40 2.61 0.04
## mfq_26 -0.29 0.07 0.02
## support -0.43 -0.53 0.03
## gad 0.69 -0.71 0.03
## big5_ext -0.22 -0.76 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 | 6 | 1 | 1 | 15 | 5 | 3 | ||
|    Prefer not to say | 2 | 2 | 15 | 1 | ||||
|    female | 96 | 3 | 25 | 27 | 773 | 24 | 26 | 19 |
|    male | 21 | 2 | 2 | 158 | 3 | 5 | 3 | |
|    #Total cases | 125 | 6 | 28 | 29 | 961 | 27 | 37 | 25 |
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$support, d2$gad,
main="Scatterplot of Variable5 and Variable8",
xlab = "Social Support Measure",
ylab = "General Anxiety Disorder-7")
plot(d2$big5_ext, d2$mfq_26,
main="Scatterplot of Variable10 and Variable11",
xlab = "Big 5 Personality Inventory",
ylab = "Mental Flexibility Questionnaire")
# 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, mfq_26~gender,
main="Boxplot of Variable2 and Variable5",
xlab = "Gender",
ylab = "Mental Flexibility Questionnaire")
boxplot(data=d2, big5_ext~mhealth,
main="Boxplot of Variable3 and Variable11",
xlab = "Mental Health",
ylab = "Big 5 Personality Inventory")
# 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!!