# 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 select rows from data: rows(mtcars, am==0)
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## To return to the console output, use 'expss_output_default()'.
##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
## 244 1 33 2
table(d2$age)
##
## 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 5 over 45
## 3 6 89 182
# use histograms to visualize continuous data
hist(d2$pas_covid)
hist(d2$support)
hist(d2$mfq_state)
hist(d2$isolation_a)
describe(d2)
## vars n mean sd median trimmed mad min max
## X 1 280 3501.69 2165.64 3433.50 3388.21 2428.50 31.00 8812.0
## gender* 2 280 1.26 0.69 1.00 1.07 0.00 1.00 4.0
## age* 3 280 3.61 0.59 4.00 3.69 0.00 1.00 4.0
## pas_covid 4 280 3.19 0.70 3.22 3.20 0.66 1.33 5.0
## support 5 280 3.87 0.88 4.00 3.95 0.74 1.17 5.0
## mfq_state 6 280 4.57 0.78 4.75 4.61 0.74 1.38 6.0
## isolation_a 7 280 1.70 0.72 1.50 1.60 0.74 1.00 3.5
## range skew kurtosis se
## X 8781.00 0.40 -0.45 129.42
## gender* 3.00 2.31 3.64 0.04
## age* 3.00 -1.52 2.79 0.04
## pas_covid 3.67 0.01 -0.07 0.04
## support 3.83 -0.78 -0.09 0.05
## mfq_state 4.62 -0.72 1.08 0.05
## isolation_a 2.50 0.85 -0.40 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.
#install.packages("maditr")
library(maditr)
cross_cases(d2,gender,age)
|  age | ||||
|---|---|---|---|---|
| Â 2 between 18 and 25Â | Â 3 between 26 and 35Â | Â 4 between 36 and 45Â | Â 5 over 45Â | |
|  gender | ||||
|    I use another term | 1 | |||
|    Prefer not to say | 2 | |||
|    female | 2 | 6 | 80 | 156 |
|    male | 1 | 8 | 24 | |
|    #Total cases | 3 | 6 | 89 | 182 |
xtabs(~gender+age,data=d2)
## age
## gender 2 between 18 and 25 3 between 26 and 35
## female 2 6
## I use another term 0 0
## male 1 0
## Prefer not to say 0 0
## age
## gender 4 between 36 and 45 5 over 45
## female 80 156
## I use another term 1 0
## male 8 24
## Prefer not to say 0 2
# 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$pas_covid,
main="Scatterplot of Social Support and Pandemic Anxiety",
xlab = "Social Support",
ylab = "Pandemic Anxiety")
plot(d2$mfq_state,d2$pas_covid,
main="Scatterplot of Mental Flexibility and Pandemic Anxiety",
xlab = "Mental Flexibility",
ylab = "Pandemic Anxiety")
# 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",
xlab = "Age",
ylab = "Pandemic Anxiety")
boxplot(data=d2, pas_covid~gender,
main="Boxplot of Gender and Pandemic Anxiety",
xlab = "Gender",
ylab = "Pandemic Anxiety")
# 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!!