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")
#install.packages("maditr")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
library(maditr)

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    788 obs. of  7 variables:
##  $ ResponseID: chr  "R_12G7bIqN2wB2N65" "R_3lLnoV2mYVYHFvf" "R_1gTNDGWsqikPuEX" "R_2QLjdu3yoxqQ21c" ...
##  $ race_rc   : chr  "white" "white" "white" "other" ...
##  $ disability: chr  "psychiatric" "other" "learning" "psychiatric" ...
##  $ pipwd     : num  2.33 2.33 3 2.93 3.33 ...
##  $ npi       : num  0.0769 0.7692 0 0.0769 0.6923 ...
##  $ exploit   : num  4.33 7 1.67 2.67 1.33 ...
##  $ stress    : num  4 3.5 4.4 3.6 3 3 3.7 3.4 2.7 3.8 ...
# 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$race_rc)
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##          38          52          45          86           2          19 
##       white 
##         546
table(d2$disability)
## 
## chronic health       learning          other       physical    psychiatric 
##            138            113             76             44            354 
##        sensory 
##             63
# use histograms to visualize continuous data (4 variables)
hist(d2$pipwd)

hist(d2$npi)

hist(d2$exploit)

hist(d2$stress)

Univariate Normality for Continuous Variables

describe(d2)
##             vars   n   mean     sd median trimmed    mad  min   max  range
## ResponseID*    1 788 394.50 227.62 394.50  394.50 292.07 1.00 788.0 787.00
## race_rc*       2 788   5.80   1.97   7.00    6.18   0.00 1.00   7.0   6.00
## disability*    3 788   3.70   1.71   5.00    3.77   1.48 1.00   6.0   5.00
## pipwd          4 788   2.92   0.70   3.00    2.93   0.69 1.13   5.0   3.87
## npi            5 788   0.28   0.30   0.15    0.24   0.23 0.00   1.0   1.00
## exploit        6 788   2.34   1.32   2.00    2.17   1.48 1.00   7.0   6.00
## stress         7 788   3.25   0.62   3.20    3.25   0.74 1.60   4.6   3.00
##              skew kurtosis   se
## ResponseID*  0.00    -1.20 8.11
## race_rc*    -1.26     0.02 0.07
## disability* -0.44    -1.36 0.06
## pipwd        0.00    -0.08 0.03
## npi          0.88    -0.84 0.01
## exploit      0.98     0.52 0.05
## stress      -0.06    -0.45 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 (1) were within the accepted range (-2/+2). However, (3) variables (stress,exploit,and narcissistic personality inventory) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice..

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, race_rc, disability)
 disability 
 chronic health   learning   other   physical   psychiatric   sensory 
 race_rc 
   asian  6 6 5 1 12 8
   black  12 5 9 7 11 8
   hispanic  9 9 3 5 16 3
   multiracial  20 11 7 3 35 10
   nativeamer  1 1
   other  4 3 3 2 6 1
   white  87 78 49 25 274 33
   #Total cases  138 113 76 44 354 63
## 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$pipwd, d2$stress, 
     main="Scatterplot of Postive Identity as a Person With a Disability and Stress ",
     xlab = "Postive Identity as a Person With a Disability",
     ylab = "Stress")

plot(d2$npi, d2$exploit,
     main="Scatterplot of Narcissistic Personality Inventory and Interpersonal Exploitativeness Scale",
     xlab = "Narcissistic Personality Inventory",
     ylab = "Interpersonal Exploitativeness Scale")

# 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, pipwd~disability,
        main="Boxplot of Postive Identity as a Person With a Disability by Disability",
        xlab = "Disability",
        ylab = "Postive Identity as a Person With a Disability")

boxplot(data=d2, stress~race_rc,
        main="Boxplot  of Perceived Stress Questionnaire by Race/Ethnicity ",
        xlab = "Race/Ethnicity",
        ylab = "Perceived Stress Questionnaire")

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