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("expss")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To select columns from data: columns(mtcars, mpg, vs:carb)

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    2155 obs. of  7 variables:
##  $ ResponseID: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
##  $ swb       : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ mindful   : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ socmeduse : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ age       : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
##  $ edu       : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
# 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 
##                1984                 115                  38                  18
table(d2$edu)
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 38 
##                             2 Currently in college 
##                                               1777 
## 3 Completed some college, but no longer in college 
##                                                 24 
##                   4 Complete 2 year College degree 
##                                                133 
##                       5 Completed Bachelors Degree 
##                                                 74 
##                  6 Currently in graduate education 
##                                                 78 
##                   7 Completed some graduate degree 
##                                                 31
# use histograms to visualize continuous data (4 variables)
hist(d2$stress)

hist(d2$swb)

hist(d2$mindful)

hist(d2$socmeduse)

Univariate Normality for Continuous Variables

describe(d2)
##             vars    n    mean     sd  median trimmed    mad   min    max
## ResponseID*    1 2155 1078.00 622.24 1078.00 1078.00 799.12  1.00 2155.0
## stress         2 2155    3.06   0.60    3.10    3.06   0.59  1.30    4.6
## swb            3 2155    4.43   1.33    4.50    4.49   1.48  1.00    7.0
## mindful        4 2155    3.72   0.84    3.73    3.72   0.79  1.13    6.0
## socmeduse      5 2155   34.25   8.59   35.00   34.52   7.41 11.00   55.0
## age*           6 2155    1.11   0.43    1.00    1.00   0.00  1.00    4.0
## edu*           7 2155    2.44   1.15    2.00    2.13   0.00  1.00    7.0
##               range  skew kurtosis    se
## ResponseID* 2154.00  0.00    -1.20 13.40
## stress         3.30 -0.01    -0.15  0.01
## swb            6.00 -0.35    -0.49  0.03
## mindful        4.87 -0.04    -0.15  0.02
## socmeduse     44.00 -0.31     0.20  0.18
## age*           3.00  4.42    21.10  0.01
## edu*           6.00  2.40     4.89  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 (change to this in hw if not true for variables)
# 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, age, edu)
 edu 
 1 High school diploma or less, and NO COLLEGE   2 Currently in college   3 Completed some college, but no longer in college   4 Complete 2 year College degree   5 Completed Bachelors Degree   6 Currently in graduate education   7 Completed some graduate degree 
 age 
   1 between 18 and 25  33 1696 21 109 44 64 17
   2 between 26 and 35  4 60 1 14 19 9 8
   3 between 36 and 45  1 15 2 6 7 4 3
   4 over 45  6 4 4 1 3
   #Total cases  38 1777 24 133 74 78 31
## 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$mindful, d2$swb, 
     main="Scatterplot of Mindfulness and Satisfaction With Life",
     xlab = "Mindfulness",
     ylab = "Satisfaction with Life")

plot(d2$mindful, d2$stress,
     main="Scatterplot of Mindfulness and Stress Levels",
     xlab = "Mindfulness",
     ylab = "Stress Levels")

# 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, mindful~age,
        main="Boxplot of Mindfulness by Age",
        xlab = "Age",
        ylab = "Mindfulness")

boxplot(data=d2, socmeduse~age,
        main="Boxplot of Social Media Use by Age",
        xlab = "Age",
        ylab = "Social Media Use")

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