library(MASS)
library(psy)
library(car)
## Warning: package 'car' was built under R version 3.4.3
library(pvclust)
## Warning: package 'pvclust' was built under R version 3.4.3
library(mapproj) # map
## Warning: package 'mapproj' was built under R version 3.4.2
## Loading required package: maps
## Warning: package 'maps' was built under R version 3.4.2
library(reshape2) # melt
## Warning: package 'reshape2' was built under R version 3.4.3
library(nparcomp) # gao_cs
## Warning: package 'nparcomp' was built under R version 3.4.2
## Loading required package: multcomp
## Warning: package 'multcomp' was built under R version 3.4.3
## Loading required package: mvtnorm
## Warning: package 'mvtnorm' was built under R version 3.4.3
## Loading required package: survival
## Loading required package: TH.data
## Warning: package 'TH.data' was built under R version 3.4.2
##
## Attaching package: 'TH.data'
## The following object is masked from 'package:MASS':
##
## geyser
library(car) # leveneTest and Anova Type III
library(heplots) # etasquared
## Warning: package 'heplots' was built under R version 3.4.2
library(MASS) # lda
library(psy) # cronbach
library(igraph) # network graphs
## Warning: package 'igraph' was built under R version 3.4.2
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
library(lsr) # partial eta squared
library(psych) # KMO
##
## Attaching package: 'psych'
## The following object is masked from 'package:car':
##
## logit
## The following object is masked from 'package:psy':
##
## wkappa
library(biotools) # M Box test
## Warning: package 'biotools' was built under R version 3.4.2
## Loading required package: rpanel
## Warning: package 'rpanel' was built under R version 3.4.2
## Loading required package: tcltk
## Package `rpanel', version 1.1-3: type help(rpanel) for summary information
## Loading required package: tkrplot
## Loading required package: lattice
## Loading required package: SpatialEpi
## Warning: package 'SpatialEpi' was built under R version 3.4.2
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.4.3
##
## Attaching package: 'SpatialEpi'
## The following object is masked from 'package:igraph':
##
## normalize
## ---
## biotools version 3.1
##
##
## Attaching package: 'biotools'
## The following object is masked from 'package:heplots':
##
## boxM
library(vcd) # goodfit
## Warning: package 'vcd' was built under R version 3.4.3
## Loading required package: grid
library(agricolae)
## Warning: package 'agricolae' was built under R version 3.4.2
##
## Attaching package: 'agricolae'
## The following object is masked from 'package:igraph':
##
## similarity
library(lavaan) # SEM4
## Warning: package 'lavaan' was built under R version 3.4.2
## This is lavaan 0.5-23.1097
## lavaan is BETA software! Please report any bugs.
##
## Attaching package: 'lavaan'
## The following object is masked from 'package:psych':
##
## cor2cov
library(Hmisc) # correlation matrix
## Warning: package 'Hmisc' was built under R version 3.4.3
## Loading required package: Formula
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.3
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
##
## Attaching package: 'Hmisc'
## The following object is masked from 'package:psych':
##
## describe
## The following objects are masked from 'package:base':
##
## format.pval, units
library(plyr) # count
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:Hmisc':
##
## is.discrete, summarize
## The following object is masked from 'package:maps':
##
## ozone
library(ggplot2)
rm(list = ls()) ## clears global environment
cat("\014") ## clears screen
setwd("C:/users/Erik Ernesto Vazquez/Downloads")
MainStudy<-read.csv("StudentsBI.csv", header=T) # reads raw data from Qualtrics
shapiro.test(MainStudy$Weighted.Total) ## normal distribution OK
##
## Shapiro-Wilk normality test
##
## data: MainStudy$Weighted.Total
## W = 0.96955, p-value = 0.4309
qqPlot(MainStudy$Weighted.Total) ## normal distribution OK

plot(density(MainStudy$Weighted.Total)) ## normal distribution OK

mydata<-data.frame(MainStudy$Weighted.Total)
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) ## At least two components
for (i in 2:15) wss[i] <- sum(kmeans(mydata,
centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")

wss<-wss/sum(wss)*100
for (i in 2:15)
wss[i]<-wss[i]+wss[i-1]
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="% Var explained")
wss
## [1] 59.18420 76.91450 82.75824 87.56303 90.17611 94.04963 95.74243
## [8] 96.55144 97.43157 98.12389 98.68180 98.92861 99.53186 99.78103
## [15] 100.00000
abline(v=3,lty=2) # 3 clusters explain more than 80% of the variance

d <- dist(mydata,method="euclidean") # distance matrix
fit <- hclust(d, method="ward")
## The "ward" method has been renamed to "ward.D"; note new "ward.D2"
plot(fit) # display dendogram all raw data
groups <- cutree(fit,k=3) # cut tree into 3 clusters
rect.hclust(fit,k=3,border="red") # draw dendogram with red borders around the 3 clusters
