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 rows from data: rows(mtcars, am==0)
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    3159 obs. of  7 variables:
##  $ ResponseID: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ marriage5 : chr  "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
##  $ gender    : chr  "f" "m" "m" "f" ...
##  $ socmeduse : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ support   : num  6 6.75 5.17 5.58 6 ...
##  $ belong    : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ efficacy  : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.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$marriage5)
## 
##             are currently divorced from one another 
##                                                 733 
##                are currently married to one another 
##                                                2132 
##       never married each other and are not together 
##                                                 247 
## never married each other but are currently together 
##                                                  47
table(d2$gender)
## 
##    f    m   nb 
## 2318  788   53
# use histograms to visualize continuous data (4 variables)
hist(d2$socmeduse)

hist(d2$support)

hist(d2$belong)

hist(d2$efficacy)

Univariate Normality for Continuous Variables

describe(d2)
##             vars    n    mean     sd  median trimmed     mad  min  max  range
## ResponseID*    1 3159 1580.00 912.07 1580.00 1580.00 1171.25  1.0 3159 3158.0
## marriage5*     2 3159    1.88   0.60    2.00    1.83    0.00  1.0    4    3.0
## gender*        3 3159    1.28   0.49    1.00    1.21    0.00  1.0    3    2.0
## socmeduse      4 3159   34.43   8.56   35.00   34.71    7.41 11.0   55   44.0
## support        5 3159    5.53   1.13    5.75    5.66    0.99  0.0    7    7.0
## belong         6 3159    3.23   0.61    3.30    3.25    0.59  1.3    5    3.7
## efficacy       7 3159    3.13   0.45    3.10    3.13    0.44  1.1    4    2.9
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.23
## marriage5*   0.47     1.49  0.01
## gender*      1.39     0.87  0.01
## socmeduse   -0.31     0.26  0.15
## support     -1.10     1.44  0.02
## belong      -0.26    -0.12  0.01
## efficacy    -0.26     0.52  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 all were within the accepted range (-2/+2).

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, marriage5, gender)
 gender 
 f   m   nb 
 marriage5 
   are currently divorced from one another  546 173 14
   are currently married to one another  1555 544 33
   never married each other and are not together  183 58 6
   never married each other but are currently together  34 13
   #Total cases  2318 788 53
## 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$socmeduse, d2$belong,
     main="Scatterplot of Social Media Use and Need to Belong",
     xlab = "Social Media Use",
     ylab = "Need to Belong")

plot(d2$support, d2$efficacy,
     main="Scatterplot of Perceived Social Support and General Self Efficacy",
     xlab = "Perceived Social Support",
     ylab = "General Self Efficacy")

# 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, support~marriage5,
        main="Boxplot of Perceived Social Support by Parents' Marital Status",
        xlab = "Parents' Marital Status",
        ylab = "Perceived Social Support")

boxplot(data=d2, efficacy~gender,
        main="Boxplot of General Self Efficacy by Gender",
        xlab = "Gender",
        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!!