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$gender)
## 
##   f   m  nb 
## 637 181  36
table(d2$disability)
## 
## chronic health       learning          other       physical    psychiatric 
##            146            121             87             50            380 
##        sensory 
##             70
# use histograms to visualize continuous data
hist(d2$mindful)

hist(d2$socmeduse)

hist(d2$efficacy)

hist(d2$moa_maturity)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##              vars   n   mean     sd median trimmed    mad   min    max  range
## ResponseID*     1 854 427.50 246.67 427.50  427.50 316.54  1.00 854.00 853.00
## gender*         2 854   1.30   0.54   1.00    1.19   0.00  1.00   3.00   2.00
## disability*     3 854   3.71   1.70   5.00    3.79   1.48  1.00   6.00   5.00
## mindful         4 854   3.47   0.84   3.47    3.48   0.79  1.13   5.67   4.53
## socmeduse       5 854  34.17   8.62  35.00   34.43   7.41 11.00  55.00  44.00
## efficacy        6 854   3.04   0.49   3.00    3.06   0.44  1.10   4.00   2.90
## moa_maturity    7 854   3.58   0.41   3.67    3.63   0.49  2.00   4.00   2.00
##               skew kurtosis   se
## ResponseID*   0.00    -1.20 8.44
## gender*       1.66     1.81 0.02
## disability*  -0.45    -1.34 0.06
## mindful      -0.05    -0.18 0.03
## socmeduse    -0.30     0.20 0.30
## efficacy     -0.42     0.50 0.02
## moa_maturity -0.98     0.78 0.01
## 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.

cross_cases(d2, gender, disability)
 disability 
 chronic health   learning   other   physical   psychiatric   sensory 
 gender 
   f  116 82 61 38 299 41
   m  26 36 23 11 57 28
   nb  4 3 3 1 24 1
   #Total cases  146 121 87 50 380 70
# 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$mindful, d2$socmeduse,
     main="Scatterplot of mindful and socmeduse",
     xlab = "mindful",
     ylab = "socmeduse")

plot(d2$efficacy, d2$moa_maturity,
     main="Scatterplot of Efficacy and Maturity",
     xlab = "efficacy",
     ylab = "moa_maturity")

# 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, moa_maturity~disability, 
        main="Boxplot of Disability and Maturity",
        xlab = "disability",
        ylab = "moa_maturity")

boxplot(data=d2, socmeduse~gender,
        main="Boxplot of gender and social media use",
        xlab = "gender",
        ylab = "socmeduse")

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