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
## Warning: package 'expss' was built under R version 4.3.3
## Loading required package: maditr
## 
## To modify variables or add new variables:
##              let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()

Import & Examine Data

# Import the "Data/projectdata.csv" file

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

str(d2)
## 'data.frame':    2162 obs. of  7 variables:
##  $ ResponseID: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender    : chr  "f" "m" "m" "f" ...
##  $ age       : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
##  $ socmeduse : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ support   : num  6 6.75 5.17 5.58 6 ...
##  $ 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 ...
# 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$gender)
## 
##    f    m   nb 
## 1587  544   31
table(d2$age)
## 
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                1990                 116                  38                  18
# use histograms to visualize continuous data (4 variables)
hist(d2$socmeduse)

hist(d2$support)

hist(d2$swb)

hist(d2$belong)

Univariate Normality for Continuous Variables

describe(d2)
##             vars    n    mean     sd  median trimmed    mad  min  max  range
## ResponseID*    1 2162 1081.50 624.26 1081.50 1081.50 801.35  1.0 2162 2161.0
## gender*        2 2162    1.28   0.48    1.00    1.21   0.00  1.0    3    2.0
## age*           3 2162    1.11   0.43    1.00    1.00   0.00  1.0    4    3.0
## socmeduse      4 2162   34.25   8.60   35.00   34.52   7.41 11.0   55   44.0
## support        5 2162    5.53   1.13    5.75    5.65   0.99  0.0    7    7.0
## swb            6 2162    4.43   1.33    4.50    4.49   1.48  1.0    7    6.0
## belong         7 2162    3.21   0.61    3.20    3.23   0.59  1.3    5    3.7
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 13.43
## gender*      1.36     0.72  0.01
## age*         4.41    21.08  0.01
## socmeduse   -0.30     0.19  0.18
## support     -1.08     1.30  0.02
## swb         -0.35    -0.49  0.03
## belong      -0.27    -0.10  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 (socmeduse, support, swb, and belong) 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, gender, age) *use other function*

## 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(~ gender + age, data=d2)
##       age
## gender 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45 4 over 45
##     f                 1477                  70                  28        12
##     m                  483                  46                   9         6
##     nb                  30                   0                   1         0
# 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$belong, d2$socmeduse,
     main="Scatterplot of Need to Belong and Social Media Use",
     xlab = "Need to Belong",
     ylab = "Social Media Use")

plot(d2$support, d2$swb,
     main="Scatterplot of Perceived Social Support and Satisfaction with Life",
     xlab = "Perceived Social Support",
     ylab = "Satisfaction with Life")

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

I plotted an extra combo of 2 continuous variables because I was curious about the relationship between them.

plot(d2$support, d2$socmeduse,
    main="Scatterplot of Perceived Social Support and Social Media Use",
    xlab = "Perceived Social Support",
    ylab = "Social Media Use")

Boxplots

Boxplots are used to visualize combinations of one categorical and one continuous variable.

# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable' 

boxplot(data=d2, belong~age,
        main="Boxplot of Need to Belong by Age",
        xlab = "Age",
        ylab = "Need to Belong")

boxplot(data=d2, support~gender,
        main="Boxplot of Perceived Social Support by Gender",
        xlab = "Gender",
        ylab = "Perceived Social Support")

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