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
## Warning: package 'psych' was built under R version 4.4.3
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.4.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.4.3
## 
## To select rows from data: rows(mtcars, am==0)

##Import Data

# Import the "fakedata.csv" file

d2 <- read.csv("Data/projectdata.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$relationship_status)
## 
##   In a relationship/married and cohabiting 
##                                        263 
## In a relationship/married but living apart 
##                                         14 
##                          Prefer not to say 
##                                          6 
##                Single, divorced or widowed 
##                                         34 
##                      Single, never married 
##                                         11
table(d2$income)
## 
##             1 low          2 middle            3 high prefer not to say 
##                37               156                87                48
# use histograms to visualize continuous data
hist(d2$iou)

hist(d2$mfq_26)

hist(d2$gad)

hist(d2$edeq12)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##                      vars   n    mean      sd  median trimmed     mad  min
## X                       1 328 3522.71 2201.99 3460.00 3396.20 2450.00 1.00
## relationship_status*    2 328    1.52    1.15    1.00    1.25    0.00 1.00
## income*                 3 328    2.45    0.88    2.00    2.43    1.48 1.00
## iou                     4 328    2.18    0.68    2.11    2.13    0.71 1.04
## mfq_26                  5 328    4.53    0.60    4.57    4.56    0.56 2.40
## gad                     6 328    1.54    0.61    1.43    1.43    0.42 1.00
## edeq12                  7 328    1.67    0.53    1.58    1.61    0.49 1.00
##                          max   range  skew kurtosis     se
## X                    8812.00 8811.00  0.40    -0.46 121.58
## relationship_status*    5.00    4.00  1.94     2.17   0.06
## income*                 4.00    3.00  0.32    -0.64   0.05
## iou                     4.44    3.41  0.67    -0.01   0.04
## mfq_26                  5.95    3.55 -0.42     0.22   0.03
## gad                     4.00    3.00  1.64     2.72   0.03
## edeq12                  3.75    2.75  1.00     0.85   0.03
## 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.

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.

#install.packages("maditr")
#library(maditr)
xtabs(~ relationship_status + income, data=d2)
##                                             income
## relationship_status                          1 low 2 middle 3 high
##   In a relationship/married and cohabiting      17      128     80
##   In a relationship/married but living apart     6        4      1
##   Prefer not to say                              1        3      0
##   Single, divorced or widowed                   12       14      4
##   Single, never married                          1        7      2
##                                             income
## relationship_status                          prefer not to say
##   In a relationship/married and cohabiting                  38
##   In a relationship/married but living apart                 3
##   Prefer not to say                                          2
##   Single, divorced or widowed                                4
##   Single, never married                                      1
# 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$gad, d2$edeq12,
     main="Scatterplot of Generalized Anxiety Disorder and Eating Disorder",
     xlab = "Generalized Anxiety Disorder",
     ylab = "Eating Disorder")

plot(d2$mfq_26, d2$iou,
     main="Scatterplot of Mental Flexibility and Intolerance of Uncertainty Scale",
     xlab = "Mental Flexibility",
     ylab = "Intolerance of Uncertainty Scale")

# 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, iou~income,
        main="Boxplot of Intolerance of Uncertainty Scale and Income",
        xlab = "Intolerance of Uncertainty Scale",
        ylab = "Income")

boxplot(data=d2, gad~relationship_status,
        main="Boxplot of Generalized Anxiety Disorder and Relationship Status",
        xlab = "Generalized Anxiety Disorder",
        ylab = "Relationship Status")

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