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 aggregate data: take(mtcars, mean_mpg = mean(mpg), by = am)
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

##Import Data

# Import the "projectdata.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

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$age)
## 
##          1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 
##                 636                  57                   6                  88 
##           5 over 45 
##                 192
table(d2$education)
## 
##              1 equivalent to not completing high school 
##                                                     284 
##                  2 equivalent to high school completion 
##                                                     282 
## 3 equivalent to vocational/technical program completion 
##                                                      21 
##                        4 equivalent to AP/IB completion 
##                                                     114 
##                                  5 undergraduate degree 
##                                                     123 
##                             6 graduate degree or higher 
##                                                      93 
##                                       prefer not to say 
##                                                      62
# use histograms to visualize continuous data
hist(d2$phq)

hist(d2$gad)

hist(d2$covid_neg)

hist(d2$pas_covid)

describe(d2)
##            vars   n    mean      sd  median trimmed     mad min  max range
## X             1 979 4698.20 2572.33 4838.00 4749.88 3309.16  20 8860  8840
## age*          2 979    2.12    1.66    1.00    1.91    0.00   1    5     4
## education*    3 979    3.04    1.98    2.00    2.84    1.48   1    7     6
## phq           4 979    2.05    0.86    1.89    1.96    0.99   1    4     3
## gad           5 979    1.99    0.90    1.71    1.90    0.85   1    4     3
## covid_neg     6 979    1.04    1.76    0.00    0.68    0.00   0    8     8
## pas_covid     7 979    3.23    0.69    3.22    3.25    0.66   1    5     4
##             skew kurtosis    se
## X          -0.13    -1.21 82.21
## age*        0.94    -0.97  0.05
## education*  0.60    -1.05  0.06
## phq         0.68    -0.58  0.03
## gad         0.75    -0.62  0.03
## covid_neg   1.50     1.11  0.06
## pas_covid  -0.20     0.11  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, age, education)
 education 
 1 equivalent to not completing high school   2 equivalent to high school completion   3 equivalent to vocational/technical program completion   4 equivalent to AP/IB completion   5 undergraduate degree   6 graduate degree or higher   prefer not to say 
 age 
   1 under 18  277 254 2 48 55
   2 between 18 and 25  8 1 45 2 1
   3 between 26 and 35  2 1 1 1 1
   4 between 36 and 45  2 6 6 6 38 29 1
   5 over 45  3 14 12 14 82 63 4
   #Total cases  284 282 21 114 123 93 62
# 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$phq, d2$gad,
     main="Scatterplot of Measures of Depression and Levels of Anxiety",
     xlab = "Depression",
     ylab = "Anxiety")

plot(d2$covid_neg, d2$pas_covid,
     main="Scatterplot of Negative Effects from COVID-19 and Pandemic Anxiety Scale",
     xlab = "Negative Effects",
     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, covid_neg~age,
        main="Boxplot of Age and Negative Effects from COVID-19",
        xlab = "Age",
        ylab = "Negative Effects")

boxplot(data=d2, phq~education,
        main="Boxplot of Level of Education and Level of Depression Severity",
        xlab = "Level of Education",
        ylab = "Depression Severity")

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