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 "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$gender)
## 
##             female I use another term               male  Prefer not to say 
##               1050                 35                217                 24
table(d2$treatment)
## 
##                    in treatment      no psychological disorders 
##                              97                             485 
##                not in treatment                           other 
##                             613                              16 
##               seeking treatment treatment disrupted by COVID-19 
##                              50                              65
# use histograms to visualize continuous data
hist(d2$big5_open)

hist(d2$big5_con)

hist(d2$big5_neu)

hist(d2$iou)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##            vars    n    mean      sd  median trimmed     mad min  max range
## X             1 1326 4825.98 2595.50 5092.50 4911.18 3286.18   1 8858  8857
## gender*       2 1326    1.41    0.82    1.00    1.24    0.00   1    4     3
## treatment*    3 1326    2.72    1.09    3.00    2.58    1.48   1    6     5
## big5_open     4 1326    5.21    1.15    5.33    5.30    0.99   1    7     6
## big5_con      5 1326    4.80    1.19    4.67    4.84    1.48   1    7     6
## big5_neu      6 1326    4.44    1.51    4.67    4.50    1.48   1    7     6
## iou           7 1326    2.61    0.92    2.48    2.56    1.04   1    5     4
##             skew kurtosis    se
## X          -0.22    -1.22 71.28
## gender*     1.66     1.12  0.02
## treatment*  1.31     2.29  0.03
## big5_open  -0.74     0.42  0.03
## big5_con   -0.26    -0.30  0.03
## big5_neu   -0.33    -0.74  0.04
## iou         0.46    -0.61  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 (5) were within the accepted range (-2/+2). However, (1) variables (treatment) 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, gender, treatment)
 treatment 
 in treatment   no psychological disorders   not in treatment   other   seeking treatment   treatment disrupted by COVID-19 
 gender 
   I use another term  8 8 9 2 2 6
   Prefer not to say  1 6 13 2 2
   female  81 397 473 12 38 49
   male  7 74 118 2 8 8
   #Total cases  97 485 613 16 50 65
# 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$big5_open, d2$big5_con,
     main="Scatterplot of Big5_open and Big5_con",
     xlab = "Big5_con",
     ylab = "Big5_open")

plot(d2$big5_neu, d2$iou,
     main="Scatterplot of Big5_neu and Iou",
     xlab = "Big5_neu",
     ylab = "Iou")

# 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, big5_con~treatment,
        main="Boxplot of Treatment and Big5_con",
        xlab = "Treatment",
        ylab = "Big5_con")

boxplot(data=d2, iou~gender,
        main="Boxplot of Gender and Iou",
        xlab = "Gender",
        ylab = "Iou")

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