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 drop variable use NULL: let(mtcars, am = NULL) %>% head()

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    2092 obs. of  7 variables:
##  $ ResponseID      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ age             : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
##  $ income          : chr  "1 low" "1 low" "rather not say" "rather not say" ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ efficacy        : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ idea            : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ stress          : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
# 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$age)
## 
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                1928                 110                  37                  17
table(d2$income)
## 
##          1 low       2 middle         3 high rather not say 
##            581            605            339            567
# use histograms to visualize continuous data (4 variables)
hist(d2$moa_independence)

hist(d2$efficacy)

hist(d2$idea)

hist(d2$stress)

Univariate Normality for Continuous Variables

describe(d2)
##                  vars    n    mean     sd  median trimmed    mad min    max
## ResponseID*         1 2092 1046.50 604.05 1046.50 1046.50 775.40 1.0 2092.0
## age*                2 2092    1.11   0.43    1.00    1.00   0.00 1.0    4.0
## income*             3 2092    2.43   1.16    2.00    2.41   1.48 1.0    4.0
## moa_independence    4 2092    3.54   0.47    3.67    3.61   0.49 1.0    4.0
## efficacy            5 2092    3.11   0.44    3.10    3.12   0.44 1.2    4.0
## idea                6 2092    3.57   0.38    3.62    3.62   0.37 1.0    4.0
## stress              7 2092    3.07   0.60    3.10    3.07   0.59 1.3    4.6
##                   range  skew kurtosis    se
## ResponseID*      2091.0  0.00    -1.20 13.21
## age*                3.0  4.44    21.31  0.01
## income*             3.0  0.17    -1.43  0.03
## moa_independence    3.0 -1.49     2.75  0.01
## efficacy            2.8 -0.20     0.39  0.01
## idea                3.0 -1.48     4.00  0.01
## stress              3.3 -0.02    -0.15  0.01
## 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 (2) were within the accepted range (-2/+2). However, (2) variables (moa_independence and idea) 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, age, income)
 income 
 1 low   2 middle   3 high   rather not say 
 age 
   1 between 18 and 25  513 546 322 547
   2 between 26 and 35  52 36 5 17
   3 between 36 and 45  11 15 9 2
   4 over 45  5 8 3 1
   #Total cases  581 605 339 567
## 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(~ age + income, data=d2)

# 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$moa_independence, d2$idea,
     main="Scatterplot of Independence and Inventory of the Dimensions of Emerging Adulthood",
     xlab = "Independence",
     ylab = "Inventory of the Dimensions of Emerging Adulthood")

plot(d2$efficacy, d2$stress,
     main="Scatterplot of General Self Efficacy Scale and Perceived Stress Questionaire",
     xlab = "General Self Efficacy Scale",
     ylab = "Perceived Stress Questionaire")

# 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, efficacy~income,
        main="Boxplot of General Self Efficacy Scale  by Income",
        xlab = "Income",
        ylab = "General Self Efficacy Scale")

boxplot(data=d2, stress~age,
        main="Boxplot of Perceived Stress Questionaire by Age",
        xlab = "Age",
        ylab = "Perceived Stress Questionaire")

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