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 select columns from data: columns(mtcars, mpg, vs:carb)
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  To return to the console output, use 'expss_output_default()'.

##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$income)
## 
##          1 low       2 middle         3 high rather not say 
##            879            876            535            855
table(d2$gender)
## 
##    f    m   nb 
## 2306  785   54
# use histograms to visualize continuous data
hist(d2$swb)

hist(d2$mindful)

hist(d2$support)

hist(d2$socmeduse)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars    n    mean     sd  median trimmed     mad   min  max   range
## ResponseID*    1 3145 1573.00 908.03 1573.00 1573.00 1165.32  1.00 3145 3144.00
## income*        2 3145    2.43   1.16    2.00    2.42    1.48  1.00    4    3.00
## gender*        3 3145    1.28   0.49    1.00    1.21    0.00  1.00    3    2.00
## swb            4 3145    4.47   1.32    4.67    4.53    1.48  1.00    7    6.00
## mindful        5 3145    3.71   0.84    3.73    3.72    0.79  1.13    6    4.87
## support        6 3145    5.54   1.13    5.75    5.66    0.99  0.00    7    7.00
## socmeduse      7 3145   34.46   8.56   35.00   34.74    7.41 11.00   55   44.00
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.19
## income*      0.14    -1.44  0.02
## gender*      1.39     0.88  0.01
## swb         -0.36    -0.45  0.02
## mindful     -0.06    -0.14  0.02
## support     -1.10     1.44  0.02
## socmeduse   -0.32     0.27  0.15
## 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, income, gender)
 gender 
 f   m   nb 
 income 
   1 low  644 218 17
   2 middle  660 203 13
   3 high  375 156 4
   rather not say  627 208 20
   #Total cases  2306 785 54
# 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$swb, d2$mindful,
     main="Scatterplot of Subjective Well-Being and Mindfulness",
     xlab = "Subjective Well-Being",
     ylab = "Mindfulness")

plot(d2$support, d2$socmeduse,
     main="Scatterplot of Support and Social Media Use",
     xlab = "Support",
     ylab = "Social Media Use")

# 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, swb~income,
        main="Boxplot of Income and Subjective Well-Being",
        xlab = "Income",
        ylab = "Subjective Well-Being")

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

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