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 get total summary skip 'by' argument: take_all(mtcars, mean)

Import & Examine Data

# Import the "fakedata_2025.csv" file

d2 <- read.csv("fakedata_2025.csv")

#describe()

# 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$grade)
## 
## level a level b level c level d level e level f 
##      36     261     379     247      53       4
table(d2$family_type)
## 
## level a level b 
##     220     760
# use histograms to visualize continuous data (4 variables)
hist(d2$social_support)

hist(d2$B5_consc)

hist(d2$B5_agree)

hist(d2$B5_extro)

Univariate Normality for Continuous Variables

## 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,grade,family_type )
 family_type 
 level a   level b 
 grade 
   level a  5 29
   level b  65 192
   level c  84 289
   level d  48 193
   level e  16 37
   level f  1 2
   #Total cases  219 742
## 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$social_support,d2$B5_consc,
     main="Scatterplot of social_support and B5_consc ",
     xlab = "social_support",
     ylab = "B5_consc")

plot(d2$B5_agree, d2$B5_extro,
     main="Scatterplot of B5_agree and B5_extro ",
     xlab = "B5_agree",
     ylab = "B5_extro")

# 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, social_support~grade,
        main = "Boxplot of social_support and grade",
        xlab = "grade",
        ylab = "social_support")

boxplot(data=d2, B5_extro~family_type,
        main = "Boxplot of family_type and B5_extro",
        xlab = "family_type",
        ylab = "B5_extro")

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