Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# delete the "#" before the install function and run it to download
# re-insert the "#" 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 aggregate all non-grouping columns: take_all(mtcars, mean, by = am)

##Import Data

#Import the "eammi2_data_final_SP25" file

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

# 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
table(d2$gender)
## 
##    f    m   nb 
## 2332  792   54
table(d2$party_rc)
## 
##  apolitical    democrat independent  republican 
##         441        1609         330         785
# use histograms to visualize continuous data
hist(d2$socmeduse)

hist(d2$stress)

hist(d2$swb)

hist(d2$belong)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##                  vars    n    mean     sd  median trimmed     mad   min    max
## ResponseID*         1 3182 1591.50 918.71 1591.50 1591.50 1179.41  1.00 3182.0
## gender*             2 3178    1.28   0.49    1.00    1.21    0.00  1.00    3.0
## race_rc*            3 3173    5.53   2.13    7.00    5.88    0.00  1.00    7.0
## age*                4 2169    1.11   0.43    1.00    1.00    0.00  1.00    4.0
## income*             5 3157    2.44   1.16    2.00    2.42    1.48  1.00    4.0
## edu*                6 3174    2.51   1.25    2.00    2.18    0.00  1.00    7.0
## sibling*            7 3182    1.10   0.29    1.00    1.00    0.00  1.00    2.0
## party_rc*           8 3165    2.46   1.01    2.00    2.45    0.00  1.00    4.0
## disability*         9  864    3.71   1.70    5.00    3.78    1.48  1.00    6.0
## marriage5*         10 3172    1.88   0.60    2.00    1.83    0.00  1.00    4.0
## phys_sym*          11 3174    2.26   0.86    3.00    2.32    0.00  1.00    3.0
## pipwd              12 1624    2.93   0.56    3.00    2.93    0.40  1.13    5.0
## moa_independence   13 3107    3.54   0.47    3.67    3.61    0.49  1.00    4.0
## moa_role           14 3111    2.97   0.72    3.00    3.00    0.74  1.00    4.0
## moa_safety         15 3123    3.20   0.64    3.25    3.26    0.74  1.00    4.0
## moa_maturity       16 3146    3.59   0.43    3.67    3.65    0.49  1.00    4.0
## idea               17 3177    3.57   0.38    3.62    3.62    0.37  1.00    4.0
## swb                18 3178    4.47   1.32    4.67    4.53    1.48  1.00    7.0
## mindful            19 3173    3.71   0.84    3.73    3.71    0.79  1.13    6.0
## belong             20 3175    3.23   0.60    3.30    3.25    0.59  1.30    5.0
## efficacy           21 3176    3.13   0.45    3.10    3.13    0.44  1.00    4.0
## support            22 3182    5.53   1.14    5.75    5.65    0.99  0.00    7.0
## socmeduse          23 3175   34.45   8.58   35.00   34.72    7.41 11.00   55.0
## usdream*           24 3171    2.39   1.55    2.00    2.24    1.48  1.00    5.0
## npi                25 3167    0.28   0.31    0.15    0.24    0.23  0.00    1.0
## exploit            26 3177    2.39   1.37    2.00    2.21    1.48  1.00    7.0
## stress             27 3176    3.05   0.60    3.00    3.05    0.59  1.30    4.7
##                    range  skew kurtosis    se
## ResponseID*      3181.00  0.00    -1.20 16.29
## gender*             2.00  1.40     0.88  0.01
## race_rc*            6.00 -0.98    -0.68  0.04
## age*                3.00  4.42    21.17  0.01
## income*             3.00  0.14    -1.44  0.02
## edu*                6.00  2.18     3.66  0.02
## sibling*            1.00  2.74     5.53  0.01
## party_rc*           3.00  0.42    -1.04  0.02
## disability*         5.00 -0.44    -1.35  0.06
## marriage5*          3.00  0.47     1.48  0.01
## phys_sym*           2.00 -0.52    -1.46  0.02
## pipwd               3.87  0.12     1.34  0.01
## moa_independence    3.00 -1.44     2.53  0.01
## moa_role            3.00 -0.33    -0.84  0.01
## moa_safety          3.00 -0.71     0.03  0.01
## moa_maturity        3.00 -1.20     1.87  0.01
## idea                3.00 -1.54     4.42  0.01
## swb                 6.00 -0.36    -0.46  0.02
## mindful             4.87 -0.06    -0.13  0.01
## belong              3.70 -0.26    -0.12  0.01
## efficacy            3.00 -0.29     0.63  0.01
## support             7.00 -1.14     1.61  0.02
## socmeduse          44.00 -0.31     0.26  0.15
## usdream*            4.00  0.62    -1.13  0.03
## npi                 1.00  0.94    -0.69  0.01
## exploit             6.00  0.95     0.37  0.02
## stress              3.40  0.04    -0.17  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.

I analyzed the skew and kurtosis of my 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, gender, party_rc)
 party_rc 
 apolitical   democrat   independent   republican 
 gender 
   f  326 1227 215 550
   m  110 343 103 234
   nb  5 37 10 1
   #Total cases  441 1607 328 785
# 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$stress,
     main="Scatterplot of Social Media Use and Stress ",
     xlab = "Social Media Use",
     ylab = "Stress")

plot(d2$swb, d2$belong,
     main="Scatterplot of Satisfaction with Life and Need to Belong",
     xlab = "Satisfaction with life",
     ylab = "Need to Belong")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your 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 variable names, not their 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, socmeduse~gender,
        main="Boxplot of  Social Media Use and Gender",
        xlab = "Gender",
        ylab = "Social Media Use")

boxplot(data=d2, stress~party_rc,
        main="Boxplot of Stress and Political Party",
        xlab = "Political Party",
        ylab = "Stress")

# 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 hypotheses. You may repeat 1 variable to see its association with others. Again, 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 variable names, not their scales, so someone reading your plots can understand them.

We did it!!