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)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by

##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 
##                                        276 
## In a relationship/married but living apart 
##                                         16 
##                          Prefer not to say 
##                                          7 
##                Single, divorced or widowed 
##                                         38 
##                      Single, never married 
##                                         12
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            28                             4 
##                    depression              eating disorders 
##                            16                             1 
##                    none or NA obsessive compulsive disorder 
##                           283                             4 
##                         other                          ptsd 
##                             7                             6
# use histograms to visualize continuous data
hist(d2$big5_ext)

hist(d2$pswq)

hist(d2$isolation_a)

hist(d2$support)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##                      vars   n    mean      sd  median trimmed     mad  min
## X                       1 349 3458.59 2183.96 3287.00 3327.51 2444.81 1.00
## relationship_status*    2 349    1.55    1.17    1.00    1.28    0.00 1.00
## mhealth*                3 349    4.65    1.31    5.00    4.90    0.00 1.00
## big5_ext                4 349    4.64    1.34    4.67    4.70    1.48 1.33
## pswq                    5 349    2.89    0.87    2.88    2.87    0.93 1.12
## isolation_a             6 349    1.68    0.71    1.50    1.59    0.74 1.00
## support                 7 349    3.86    0.88    4.00    3.94    0.74 1.00
##                          max   range  skew kurtosis     se
## X                    8803.00 8802.00  0.43    -0.43 116.90
## relationship_status*    5.00    4.00  1.86     1.86   0.06
## mhealth*                8.00    7.00 -1.51     3.20   0.07
## big5_ext                7.00    5.67 -0.35    -0.68   0.07
## pswq                    4.94    3.81  0.14    -0.82   0.05
## isolation_a             3.50    2.50  0.90    -0.28   0.04
## support                 5.00    4.00 -0.80     0.13   0.05
## For the required write-up below, choose one of these options to paste and edit below based on your output.

# We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).

# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name 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.

cross_cases(d2, relationship_status, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 relationship_status 
   In a relationship/married and cohabiting  16 2 10 236 2 6 4
   In a relationship/married but living apart  5 10 1
   Prefer not to say  1 1 5
   Single, divorced or widowed  5 1 6 23 2 1
   Single, never married  1 1 9 1
   #Total cases  28 4 16 1 283 4 7 6
# Note: for HW, replace the two variables with your project's categorical ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$big5_ext, d2$isolation_a,
     main="Scatterplot of big5_ext and isolation_a",
     xlab = "big5_ext",
     ylab = "isolation_a")

plot(d2$support, d2$isolation_a,
     main="Scatterplot of support and isolation_a",
     xlab = "support",
     ylab = "isolation_a")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your research questions/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.

Boxplots

Boxplots are used to visualize combinations of one categorical and one continuous variable.

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

boxplot(data=d2,isolation_a~relationship_status,
        main="Boxplot of elationship_status and isolation_a",
        xlab = "relationship_status",
        ylab = "isolation_a")

boxplot(data=d2, pswq~mhealth,
        main="Boxplot of mhealth and pswq",
        xlab = "mhealth",
        ylab = "pswq")

# 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 research questions/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 graph.