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("psych")
#install.packages("expss")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.3.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.3.3
## 
## To modify variables or add new variables:
##              let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()
## 
## 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':    681 obs. of  7 variables:
##  $ X                  : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ relationship_status: chr  "Single, never married" "Single, never married" "Prefer not to say" "Single, never married" ...
##  $ pet                : chr  "cat" "other" "no pets" "no pets" ...
##  $ big5_ext           : num  2 2.67 4.67 4.33 1.67 ...
##  $ pas_covid          : num  3 3.44 4.67 2.44 1.56 ...
##  $ pss                : num  2.75 2.25 3 2 1.75 2 1 1.25 3 1.25 ...
##  $ isolation_c        : num  1 1 1 1 1 1 1 1 1 1 ...
# 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$relationship_status)
## 
##   In a relationship/married and cohabiting 
##                                          1 
## In a relationship/married but living apart 
##                                         68 
##                          Prefer not to say 
##                                         68 
##                Single, divorced or widowed 
##                                          2 
##                      Single, never married 
##                                        542
table(d2$pet)
## 
##                  bird                   cat           cat and dog 
##                     3                   121                    80 
##                   dog                  fish multiple types of pet 
##                   112                    19                    62 
##               no pets                 other 
##                   244                    40
# use histograms to visualize continuous data (4 variables)
hist(d2$big5_ext)

hist(d2$pas_covid)

hist(d2$pss)

hist(d2$isolation_c)

Univariate Normality for Continuous Variables

describe(d2)
##                      vars   n    mean      sd  median trimmed     mad min
## X                       1 681 5168.62 2602.17 5722.00 5313.32 3110.49  20
## relationship_status*    2 681    4.49    1.03    5.00    4.74    0.00   1
## pet*                    3 681    5.03    2.09    6.00    5.10    2.97   1
## big5_ext                4 681    4.24    1.46    4.33    4.27    1.48   1
## pas_covid               5 681    3.24    0.68    3.33    3.26    0.66   1
## pss                     6 681    3.13    0.95    3.25    3.15    1.11   1
## isolation_c             7 681    2.29    0.82    2.25    2.29    1.11   1
##                         max  range  skew kurtosis    se
## X                    8860.0 8840.0 -0.40    -1.14 99.72
## relationship_status*    5.0    4.0 -1.66     1.05  0.04
## pet*                    8.0    7.0 -0.23    -1.51  0.08
## big5_ext                7.0    6.0 -0.18    -0.78  0.06
## pas_covid               5.0    4.0 -0.30     0.19  0.03
## pss                     5.0    4.0 -0.13    -0.74  0.04
## isolation_c             3.5    2.5 -0.02    -1.24  0.03
## 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 all were within the accepted range (-2/+2).

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, relationship_status, pet)
 pet 
 bird   cat   cat and dog   dog   fish   multiple types of pet   no pets   other 
 relationship_status 
   In a relationship/married and cohabiting  1
   In a relationship/married but living apart  13 8 16 2 8 14 7
   Prefer not to say  9 7 8 4 4 35 1
   Single, divorced or widowed  1 1
   Single, never married  3 99 64 87 13 49 195 32
   #Total cases  3 121 80 112 19 62 244 40
## 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$big5_ext, d2$pss,
     main="Scatterplot of Extroversion and Perceived Stress",
     xlab = "Extroversion",
     ylab = "Perceived Stress")

plot(d2$isolation_c, d2$pss,
     main="Scatterplot of Loneliness and Perceived Stress",
     xlab = "Loneliness",
     ylab = "Perceived Stress")

# 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, pas_covid~relationship_status,
        main="Boxplot of Social Support by Grade",
        xlab = "Relationship Status",
        ylab = "Covid Anxiety")

boxplot(data=d2, isolation_c~pet,
        main="Boxplot of Loneliness by Pet Type",
        xlab = "Pet Type",
        ylab = "Loneliness")

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