1— title: “P421 Lab - Basic Statistics Homework” author: “Annika Fought” date: “2026-02-11” output: html_document editor_options: chunk_output_type: console —

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
## Loading required package: maditr
## 
## To modify variables or add new variables:
##              let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    263 obs. of  7 variables:
##  $ X        : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ exercise : chr  "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" ...
##  $ mhealth  : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ covid_pos: int  12 1 7 5 5 9 8 3 4 9 ...
##  $ covid_neg: int  2 4 3 0 2 0 5 2 1 3 ...
##  $ big5_neu : num  6.67 4.33 2 4 3.33 ...
##  $ brs      : num  2 3.83 3.83 4 4.67 ...
# 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$exercise)
## 
## 1 less than 1 hour        2 1-2 hours        3 2-5 hours        4 5-8 hours 
##                 55                102                 74                 21 
##     5 over 8 hours 
##                 11
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            35                             3 
##                    depression              eating disorders 
##                             6                            14 
##                    none or NA obsessive compulsive disorder 
##                           177                            10 
##                         other                          ptsd 
##                            11                             7
# use histograms to visualize continuous data (4 variables)
hist(d2$covid_pos)

hist(d2$covid_neg)

hist(d2$big5_neu)

hist(d2$brs)

Univariate Normality for Continuous Variables

describe(d2)
##           vars   n    mean     sd  median trimmed    mad  min  max range  skew
## X            1 263 7565.53 754.09 7531.00 7566.48 987.41 6291 8834  2543  0.01
## exercise*    2 263    2.36   1.03    2.00    2.27   1.48    1    5     4  0.60
## mhealth*     3 263    4.54   1.62    5.00    4.67   0.00    1    8     7 -1.00
## covid_pos    4 263    5.44   3.56    5.00    5.24   4.45    0   15    15  0.44
## covid_neg    5 263    3.08   1.64    3.00    3.10   1.48    0    8     8 -0.06
## big5_neu     6 263    5.05   1.28    5.00    5.16   1.48    1    7     6 -0.69
## brs          7 263    2.68   0.85    2.67    2.68   0.99    1    5     4  0.13
##           kurtosis    se
## X            -1.28 46.50
## exercise*     0.00  0.06
## mhealth*      0.94  0.10
## covid_pos    -0.50  0.22
## covid_neg    -0.41  0.10
## big5_neu      0.00  0.08
## brs          -0.56  0.05
## 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, mhealth, exercise)
 exercise 
 1 less than 1 hour   2 1-2 hours   3 2-5 hours   4 5-8 hours   5 over 8 hours 
 mhealth 
   anxiety disorder  6 13 11 4 1
   bipolar  1 2
   depression  3 1 1 1
   eating disorders  3 3 6 1 1
   none or NA  38 70 48 13 8
   obsessive compulsive disorder  5 2 3
   other  1 7 3
   ptsd  1 4 2
   #Total cases  55 102 74 21 11
## 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_pos, d2$covid_neg,
     main="Scatterplot of Positive Effects of COVID-19 and Negative Effects of Covid-19  ",
     xlab = "Positive Effects of COVID-19",
     ylab = "Negative Effects of COVID-19")

plot(d2$big5_neu, d2$brs,
     main="Scatterplot of Neuroticism and Resilience",
     xlab = "Neuroticism",
     ylab = "Resilience")

# 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, big5_neu~mhealth,
        main="Boxplot of Neuroticism by Mental Health",
        xlab = "Mental Health",
        ylab = "Neuroticism")

boxplot(data=d2, brs~mhealth,
        main="Boxplot of Resilience by Mental Health",
        xlab = "Mental Health",
        ylab = "Resilience")

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