Basic Statistics

Load Libraries

# 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
## Warning: package 'psych' was built under R version 4.4.3
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.4.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.4.3
## 
## To get total summary skip 'by' argument: take_all(mtcars, mean)
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

##Import Data

# Import the data 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

Univariate Plots: Histograms & Tables

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 
##                951                 31                185                 18
table(d2$trans)
## 
##                no Prefer not to say               yes 
##              1108                40                37
# use histograms to visualize continuous data
hist(d2$pas_covid)

hist(d2$pss)

hist(d2$gad)

hist(d2$swemws)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##           vars    n    mean      sd  median trimmed     mad min  max range
## X            1 1185 4630.36 2571.98 4750.00 4672.53 3326.95   1 8867  8866
## gender*      2 1185    1.38    0.80    1.00    1.21    0.00   1    4     3
## trans*       3 1185    1.10    0.39    1.00    1.00    0.00   1    3     2
## pas_covid    4 1185    3.22    0.68    3.22    3.23    0.66   1    5     4
## pss          5 1185    2.91    0.95    3.00    2.90    1.11   1    5     4
## gad          6 1185    2.02    0.90    1.71    1.92    0.85   1    4     3
## swemws       7 1185    3.17    0.84    3.14    3.19    0.85   1    5     4
##            skew kurtosis    se
## X         -0.11    -1.22 74.72
## gender*    1.74     1.38  0.02
## trans*     4.14    16.36  0.01
## pas_covid -0.16     0.03  0.02
## pss        0.10    -0.75  0.03
## gad        0.71    -0.67  0.03
## swemws    -0.28    -0.25  0.02
## 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).

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, gender, trans)
 trans 
 no   Prefer not to say   yes 
 gender 
   I use another term  8 2 21
   Prefer not to say  3 14 1
   female  932 19
   male  165 5 15
   #Total cases  1108 40 37
# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$pss, d2$swems,
     main="Scatterplot of Perceived Stress and Mental Wellbeing",
     xlab = "Perceived Stress",
     ylab = "Mental Wellbeing")

plot(d2$gad, d2$pas_covid,
     main="Scatterplot of General Anxiety Disorder and Pandemic Anxiety",
     xlab = "General Anxiety Disorder",
     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

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~trans,
        main="Boxplot of Trans and Pandemic Anxiety",
        xlab = "Trans",
        ylab = "Pandemic Anxiety")

boxplot(data=d2, swemws~gender,
        main="Boxplot of Gender and Mental Wellbeing",
        xlab = "Gender",
        ylab = "Mental Wellbeing")

# 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!!