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)
library(maditr)

##Import Data

# Import the "projectdata.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$sexual_orientation)
## 
##               Asexual                    Bi           Gay/Lesbian 
##                    35                   155                    51 
## Heterosexual/Straight    I use another term     Prefer not to say 
##                   866                    34                    84
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           126                             5 
##                    depression              eating disorders 
##                            30                            31 
##                    none or NA obsessive compulsive disorder 
##                           949                            27 
##                         other                          ptsd 
##                            35                            22
# use histograms to visualize continuous data
hist(d2$iou)

hist(d2$rse)

hist(d2$big5_open)

hist(d2$gad)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##                     vars    n    mean      sd  median trimmed     mad min  max
## X                      1 1225 4719.22 2589.62 4885.00 4777.66 3331.40   1 8867
## sexual_orientation*    2 1225    3.78    1.03    4.00    3.81    0.00   1    6
## mhealth*               3 1225    4.64    1.40    5.00    4.88    0.00   1    8
## big5_open              4 1225    5.25    1.13    5.33    5.33    0.99   1    7
## iou                    5 1225    2.56    0.90    2.41    2.51    0.99   1    5
## rse                    6 1225    2.63    0.72    2.70    2.65    0.74   1    4
## gad                    7 1225    2.05    0.91    1.71    1.96    0.85   1    4
##                     range  skew kurtosis    se
## X                    8866 -0.15    -1.22 73.99
## sexual_orientation*     5 -0.51     1.20  0.03
## mhealth*                7 -1.43     2.56  0.04
## big5_open               6 -0.73     0.44  0.03
## iou                     4  0.50    -0.58  0.03
## rse                     3 -0.22    -0.73  0.02
## gad                     3  0.67    -0.74  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.

[TYPE Write-up of Normality here. THEN DELETE OUTER BRACKETS] 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, sexual_orientation, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 sexual_orientation 
   Asexual  4 1 26 2 2
   Bi  26 2 6 9 88 5 13 6
   Gay/Lesbian  11 1 1 30 3 3 2
   Heterosexual/Straight  75 1 21 17 713 15 11 13
   I use another term  3 1 1 27 1 1
   Prefer not to say  7 2 3 65 1 6
   #Total cases  126 5 30 31 949 27 35 22
# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.



``` r
plot(d2$iou, d2$rse,
     main="Scatterplot of Intolerance of Uncertainty and Rosenberg Self Esteem",
     xlab = "Intolerance of Uncertainty",
     ylab = "Rosenberg Self Esteem")

plot(d2$big5_open, d2$gad,
     main="Scatterplot of Big 5 Personality Inventory-Neuroticism Scale and General Anxiety Disorder",
     xlab = "Big 5 Personality Inventory-Neuroticism Scale",
     ylab = "General Anxiety Disorder")

# 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

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

boxplot(data=d2, rse~mhealth,
        main="Boxplot of Rosenberg Self Esteem and Mental Health",
        xlab = "Rosenberg Self Esteem",
        ylab = "Mental Health")

boxplot(data=d2, big5_open~sexual_orientation,
        main="Boxplot of Big 5 Personality Inventory-Neuroticism Scale and Sexual Orientation",
        xlab = "Big 5 Inventory-Neuroticism Scale",
        ylab = "Sexual Orientation")

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