library(readxl)
habitat <- read_excel("Biol2022MultivariateSpreadsheet (4).xlsx", 
    sheet = "Habitat Variables")

PCA

library(REdaS)
## Loading required package: grid

Correlation Matrix

cormat <- cor((habitat[1:36, 4:15]))
bart_spher(cormat)
##  Bartlett's Test of Sphericity
## 
## Call: bart_spher(x = cormat)
## 
##      X2 = 325.279
##      df = 66
## p-value < 2.22e-16
cormat
##                  carparks escalators   elevators      levels     toilets
## carparks       1.00000000  0.1104018 -0.03651974  0.27488040  0.36134990
## escalators     0.11040178  1.0000000  0.73954196  0.50882483  0.71726211
## elevators     -0.03651974  0.7395420  1.00000000  0.44497674  0.47146012
## levels         0.27488040  0.5088248  0.44497674  1.00000000  0.38411470
## toilets        0.36134990  0.7172621  0.47146012  0.38411470  1.00000000
## hours          0.12710142  0.1515900  0.09310969  0.01157244  0.08566331
## eateries       0.40250032  0.4862020  0.31493815  0.49847231  0.67115063
## trainstations  0.18535517  0.3602172  0.22379553  0.27026228  0.34410700
## busstops       0.12063463  0.1247951  0.15460860 -0.08987194 -0.02223400
## cinema         0.44525146  0.3371226  0.22596641  0.68586483  0.50876630
## greenspace    -0.05889480 -0.2018897 -0.07751053 -0.22812489 -0.14558848
## foodcourts     0.42506151  0.4548239  0.07409223  0.31467775  0.37917306
##                     hours   eateries trainstations    busstops      cinema
## carparks       0.12710142  0.4025003     0.1853552  0.12063463  0.44525146
## escalators     0.15159000  0.4862020     0.3602172  0.12479512  0.33712259
## elevators      0.09310969  0.3149381     0.2237955  0.15460860  0.22596641
## levels         0.01157244  0.4984723     0.2702623 -0.08987194  0.68586483
## toilets        0.08566331  0.6711506     0.3441070 -0.02223400  0.50876630
## hours          1.00000000  0.2923818     0.1756103  0.39344563  0.09786211
## eateries       0.29238185  1.0000000     0.2516909  0.11067248  0.69984069
## trainstations  0.17561033  0.2516909     1.0000000 -0.12707898  0.30303450
## busstops       0.39344563  0.1106725    -0.1270790  1.00000000 -0.14285714
## cinema         0.09786211  0.6998407     0.3030345 -0.14285714  1.00000000
## greenspace    -0.28375623 -0.3502501    -0.2115139 -0.17131295 -0.14151940
## foodcourts     0.10887017  0.4216912     0.3893209  0.06992765  0.34963827
##                greenspace foodcourts
## carparks      -0.05889480 0.42506151
## escalators    -0.20188969 0.45482390
## elevators     -0.07751053 0.07409223
## levels        -0.22812489 0.31467775
## toilets       -0.14558848 0.37917306
## hours         -0.28375623 0.10887017
## eateries      -0.35025006 0.42169121
## trainstations -0.21151389 0.38932091
## busstops      -0.17131295 0.06992765
## cinema        -0.14151940 0.34963827
## greenspace     1.00000000 0.06131806
## foodcourts     0.06131806 1.00000000
pca <- prcomp(habitat[1:36, 4:15], scale = TRUE)
pca
## Standard deviations (1, .., p=12):
##  [1] 2.0860132 1.2580940 1.1925535 1.0425076 0.9789424 0.8061893 0.7861824
##  [8] 0.7214278 0.6048389 0.4627482 0.3870815 0.2830795
## 
## Rotation (n x k) = (12 x 12):
##                       PC1         PC2         PC3         PC4         PC5
## carparks       0.23205954  0.08006170 -0.53709414 -0.18349764  0.18745860
## escalators     0.37647041 -0.08212495  0.39538653 -0.20704267 -0.05740025
## elevators      0.27300122 -0.12052440  0.58108673 -0.11150645  0.12595162
## levels         0.34420670  0.18233218  0.08734796  0.26895706  0.19998701
## toilets        0.38337477  0.07109288  0.11611467 -0.12179440  0.05270884
## hours          0.12893475 -0.56496165 -0.21701805  0.01693406 -0.18049314
## eateries       0.39329232 -0.06436029 -0.16694286  0.15073965  0.19820064
## trainstations  0.24797068  0.06370575 -0.02077734  0.01398219 -0.78631070
## busstops       0.04031147 -0.62617034 -0.08270978 -0.31617962  0.27664883
## cinema         0.35801789  0.24023125 -0.20646450  0.25712886  0.21796496
## greenspace    -0.15287461  0.37652796  0.05133484 -0.63211290  0.17647249
## foodcourts     0.28012375  0.12273659 -0.26253209 -0.48476825 -0.23999172
##                       PC6         PC7          PC8         PC9        PC10
## carparks      -0.10426932  0.21909361 -0.599400718  0.35133891 -0.14540896
## escalators    -0.09615255  0.09817732  0.136911448  0.26102042  0.21045333
## elevators      0.16075680 -0.03674456 -0.314890013  0.05035678 -0.54336864
## levels         0.56201880  0.22364097  0.048312960  0.17511954  0.31615851
## toilets       -0.58613158 -0.18425336 -0.093010319 -0.03136450  0.46949455
## hours          0.25076034 -0.61340324  0.014282144  0.36304628  0.09180864
## eateries      -0.24329523 -0.21431501  0.274917661 -0.30010969 -0.45743688
## trainstations  0.12951695  0.04295899 -0.344922891 -0.38884532  0.01938062
## busstops       0.14588221  0.32461422 -0.063277324 -0.49386455  0.21949563
## cinema         0.22460458 -0.23124601  0.001154652 -0.33011530  0.09672403
## greenspace     0.26463106 -0.46549395 -0.107494333 -0.17522661  0.08298754
## foodcourts     0.11839804  0.23973069  0.549105972  0.13263157 -0.19094785
##                       PC11        PC12
## carparks       0.004397920  0.14035244
## escalators     0.251269494  0.66018641
## elevators      0.099505607 -0.33309103
## levels        -0.473036881 -0.07558336
## toilets       -0.140400716 -0.43543450
## hours         -0.014913637 -0.06656343
## eateries      -0.426795672  0.29854810
## trainstations -0.130751084  0.09631381
## busstops      -0.003726279 -0.01364596
## cinema         0.659058200 -0.03532629
## greenspace    -0.194181379  0.16716628
## foodcourts     0.109159839 -0.32553445
summary(pca)
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5     PC6     PC7
## Standard deviation     2.0860 1.2581 1.1926 1.04251 0.97894 0.80619 0.78618
## Proportion of Variance 0.3626 0.1319 0.1185 0.09057 0.07986 0.05416 0.05151
## Cumulative Proportion  0.3626 0.4945 0.6130 0.70360 0.78347 0.83763 0.88913
##                            PC8     PC9    PC10    PC11    PC12
## Standard deviation     0.72143 0.60484 0.46275 0.38708 0.28308
## Proportion of Variance 0.04337 0.03049 0.01784 0.01249 0.00668
## Cumulative Proportion  0.93251 0.96299 0.98084 0.99332 1.00000

Scree Plot

screeplot(pca, type = "lines")

Principle Component Scores

pcscores <- cbind(habitat, pca$x)

Factor Analysis

fa <- factanal(habitat[1:36, 4:15], 3, rotation = "varimax")
fa
## 
## Call:
## factanal(x = habitat[1:36, 4:15], factors = 3, rotation = "varimax")
## 
## Uniquenesses:
##      carparks    escalators     elevators        levels       toilets 
##         0.706         0.005         0.435         0.390         0.348 
##         hours      eateries trainstations      busstops        cinema 
##         0.772         0.198         0.830         0.782         0.064 
##    greenspace    foodcourts 
##         0.852         0.716 
## 
## Loadings:
##               Factor1 Factor2 Factor3
## carparks               0.504   0.200 
## escalators     0.971   0.112   0.201 
## elevators      0.750                 
## levels         0.497   0.571  -0.191 
## toilets        0.629   0.411   0.296 
## hours                  0.134   0.456 
## eateries       0.331   0.717   0.423 
## trainstations  0.332   0.238         
## busstops              -0.117   0.449 
## cinema         0.265   0.923  -0.116 
## greenspace    -0.121  -0.171  -0.323 
## foodcourts     0.388   0.300   0.208 
## 
##                Factor1 Factor2 Factor3
## SS loadings      2.608   2.337   0.959
## Proportion Var   0.217   0.195   0.080
## Cumulative Var   0.217   0.412   0.492
## 
## Test of the hypothesis that 3 factors are sufficient.
## The chi square statistic is 39.88 on 33 degrees of freedom.
## The p-value is 0.191
mobility <- pcscores$PC1 
facilities <- pcscores$PC2

ANOVA

Check Assumptions

shapiro.test(mobility)
## 
##  Shapiro-Wilk normality test
## 
## data:  mobility
## W = 0.95022, p-value = 0.1063
shapiro.test(facilities)
## 
##  Shapiro-Wilk normality test
## 
## data:  facilities
## W = 0.75329, p-value = 2.179e-06

PC1 ANOVA - Mobility

aov1 <- aov(mobility ~ Income*Population, data = pcscores)
summary(aov1)
##                   Df Sum Sq Mean Sq F value Pr(>F)
## Income             1   9.03   9.031   2.033  0.164
## Population         1   0.03   0.026   0.006  0.940
## Income:Population  1   1.12   1.115   0.251  0.620
## Residuals         32 142.13   4.442

Boxplot - Mobility

library(ggplot2)

mob.box <- ggplot(pcscores, aes(y = PC1, x = Income, fill = Population)) +
  geom_boxplot(position = position_dodge(.9))
mob.box

PC2 ANOVA - Facilities

aov2 <- aov(facilities ~ Income*Population, data = pcscores)
summary(aov2)
##                   Df Sum Sq Mean Sq F value Pr(>F)
## Income             1   0.32  0.3235   0.189  0.667
## Population         1   0.02  0.0180   0.011  0.919
## Income:Population  1   0.34  0.3438   0.201  0.657
## Residuals         32  54.71  1.7098

Boxplot - Facilities

fac.box <- ggplot(pcscores, aes(y= PC2, x = Income, fill = Population)) +
  geom_boxplot(position = position_dodge(.9))
fac.box