Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# after download is finished, insert a "#" before the install function so that the file will Knit later
# then run the library function calling that package


#install.packages("expss")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To aggregate all non-grouping columns: take_all(mtcars, mean, by = am)
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  To return to the console output, use 'expss_output_default()'.

Import & Examine Data

# Import the "fakedata_2025.csv" file

d2 <- read.csv("Data/projectdata.csv")

str(d2)
## 'data.frame':    1166 obs. of  7 variables:
##  $ X        : int  1 321 401 1390 2183 2247 2526 2609 2689 2752 ...
##  $ treatment: chr  "no psychological disorders" "not in treatment" "not in treatment" "not in treatment" ...
##  $ mhealth  : chr  "none or NA" "none or NA" "obsessive compulsive disorder" "none or NA" ...
##  $ pswq     : num  4.94 1.71 2.44 2.25 3.14 ...
##  $ pas_covid: num  3.22 2.33 4 2.89 3.22 ...
##  $ covid_neg: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ covid_pos: int  0 0 0 0 0 0 0 0 0 0 ...
# 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 (2 variables)
table(d2$treatment)
## 
##                    in treatment      no psychological disorders 
##                              78                             425 
##                not in treatment                           other 
##                             548                              14 
##               seeking treatment treatment disrupted by COVID-19 
##                              46                              55
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           133                             8 
##                    depression              eating disorders 
##                            33                            30 
##                    none or NA obsessive compulsive disorder 
##                           872                            28 
##                         other                          ptsd 
##                            37                            25
# use histograms to visualize continuous data (4 variables)
hist(d2$pswq)

hist(d2$pas_covid)

hist(d2$covid_neg)

hist(d2$covid_pos)

Univariate Normality for Continuous Variables

describe(d2)
##            vars    n    mean      sd  median trimmed     mad min     max
## X             1 1166 4726.18 2590.34 4915.00 4789.81 3344.00   1 8867.00
## treatment*    2 1166    2.73    1.08    3.00    2.59    1.48   1    6.00
## mhealth*      3 1166    4.59    1.49    5.00    4.80    0.00   1    8.00
## pswq          4 1166    2.77    0.80    2.80    2.77    0.93   1    4.94
## pas_covid     5 1166    3.25    0.70    3.33    3.26    0.66   1    5.00
## covid_neg     6 1166    1.13    1.83    0.00    0.77    0.00   0    8.00
## covid_pos     7 1166    1.93    3.34    0.00    1.17    0.00   0   15.00
##              range  skew kurtosis    se
## X          8866.00 -0.17    -1.24 75.86
## treatment*    5.00  1.32     2.34  0.03
## mhealth*      7.00 -1.26     1.84  0.04
## pswq          3.94 -0.03    -0.79  0.02
## pas_covid     4.00 -0.21     0.06  0.02
## covid_neg     8.00  1.40     0.81  0.05
## covid_pos    15.00  1.71     2.01  0.10
## 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.

Write-up of Normality

We analyzed the skew and kurtosis of our continuous variables and (5) were within the accepted range (-2/+2). However, (2) variables (treatment and covid_pos) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, treatment, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 treatment 
   in treatment  22 2 4 10 11 5 18 6
   no psychological disorders  3 1 1 417 1 2
   not in treatment  75 3 18 11 404 18 8 11
   other  2 1 8 1 2
   seeking treatment  10 7 1 21 3 4
   treatment disrupted by COVID-19  21 2 2 8 11 4 5 2
   #Total cases  133 8 33 30 872 28 37 25
## Some students may have issues with this function working. If this happens to you, please try these 2 options:
## Option 1: install the "maditr" package and then call in its library.
## Option 2: If Option 1 doesn't work, then you will use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line.

# xtabs(~ + , data=)

# 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$covid_neg, d2$pas_covid,
     main="Scatterplot of Negative Effects of COVID-19 and Pandemic Anxiety Scale ",
     xlab = "Negative Effects of COVID-19",
     ylab = "Pandemic Anxiety Scale")

plot(d2$covid_pos, d2$pswq,
     main="Scatterplot of Positive Effects of COVID-19 and Penn State Worry Questionnaire",
     xlab = "Positive Effects of COVID-19",
     ylab = "Penn State Worry Questionnaire")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your potential 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 construct names, NOT their R abbrev or full 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~treatment,
        main="Boxplot of Negative Effects of COVID-19 by Mental Health Treatment",
        xlab = "Mental Health Treatment",
        ylab = "Negative Effects of COVID-19")

boxplot(data=d2, pswq~mhealth,
        main="Boxplot of Penn State Worry Questionnaire by Mental Health Disorder ",
        xlab = "Mental Health Disorder",
        ylab = "Penn State Worry Questionnaire")

# 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 potential hypotheses. You may repeat 1 variable to see its association with others. Again, 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 construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.

That’s it!!