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 select rows from data: rows(mtcars, am==0)

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    3139 obs. of  7 variables:
##  $ ResponseID: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ efficacy  : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ swb       : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ belong    : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ usdream   : chr  "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
##  $ income    : chr  "1 low" "1 low" "rather not say" "rather not say" ...
# 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$usdream)
## 
##               american dream is important and achievable for me 
##                                                            1455 
##     american dream is important but maybe not achievable for me 
##                                                             341 
## american dream is not important and maybe not achievable for me 
##                                                             584 
##        american dream is not important but is achievable for me 
##                                                             182 
##                            not sure if american dream important 
##                                                             577
table(d2$income)
## 
##          1 low       2 middle         3 high rather not say 
##            875            880            535            849
# use histograms to visualize continuous data (4 variables)
hist(d2$efficacy)

hist(d2$swb)

hist(d2$belong)

hist(d2$stress)

Univariate Normality for Continuous Variables

describe(d2)
##             vars    n    mean     sd  median trimmed     mad min    max  range
## ResponseID*    1 3139 1570.00 906.30 1570.00 1570.00 1163.84 1.0 3139.0 3138.0
## efficacy       2 3139    3.13   0.45    3.10    3.13    0.44 1.1    4.0    2.9
## swb            3 3139    4.48   1.32    4.67    4.53    1.48 1.0    7.0    6.0
## belong         4 3139    3.24   0.60    3.30    3.25    0.59 1.3    5.0    3.7
## stress         5 3139    3.05   0.60    3.00    3.05    0.59 1.3    4.7    3.4
## usdream*       6 3139    2.39   1.54    2.00    2.24    1.48 1.0    5.0    4.0
## income*        7 3139    2.43   1.16    2.00    2.42    1.48 1.0    4.0    3.0
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.18
## efficacy    -0.24     0.44  0.01
## swb         -0.36    -0.45  0.02
## belong      -0.26    -0.13  0.01
## stress       0.03    -0.17  0.01
## usdream*     0.63    -1.12  0.03
## income*      0.15    -1.43  0.02
## 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, usdream, income)
 income 
 1 low   2 middle   3 high   rather not say 
 usdream 
   american dream is important and achievable for me  379 457 290 329
   american dream is important but maybe not achievable for me  111 81 59 90
   american dream is not important and maybe not achievable for me  185 153 63 183
   american dream is not important but is achievable for me  56 46 43 37
   not sure if american dream important  144 143 80 210
   #Total cases  875 880 535 849
## 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$swb, d2$efficacy, 
     main="Scatterplot of Satisfaction with Life and General Self Efficacy",
     xlab = "Satisfaction with Life",
     ylab = "General Self Efficacy")

plot(d2$stress, d2$belong,
     main="Scatterplot of Stress and Need to Belong ",
     xlab = "Stress",
     ylab = "Need to Belong")

# 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, swb~income,
        main="Boxplot of Satisfaction with Life by Income",
        xlab = "Income",
        ylab = "Satisfaction with Life")

boxplot(data=d2, efficacy~usdream,
        main="Boxplot of General Self Efficacy by Attainability of American Dream",
        xlab = "Attainability of American Dream",
        ylab = "General Self Efficacy")

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