library(carData)
library(readxl)
library(car)
library(janitor)
##
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
library(ggplot2)
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(ggpubr)
library(ggrepel)
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ✔ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::recode() masks car::recode()
## ✖ purrr::some() masks car::some()
library(usmap)
junkins <- read_excel("C:\\Users\\anami\\OneDrive\\Documents\\Stat ll\\Assignment 2\\Junkins Data.xlsx")
head(junkins)
## # A tibble: 6 × 62
## STATE_ABBR statename state region division rz_ext rz_agr rz_cns rz_neu
## <chr> <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 AK Alabama 1 3 6 -0.126 -0.105 -0.110 -0.0491
## 2 AL Alaska 2 4 9 0.0430 0.100 0.0806 -0.0152
## 3 AR Arizona 4 4 8 -0.00934 -0.0154 -0.0583 0.0765
## 4 AZ Arkansas 5 3 7 0.0122 -0.00702 0.00843 -0.0436
## 5 CA California 6 4 9 -0.0430 -0.0357 -0.0654 -0.0143
## 6 CO Colorado 8 4 8 -0.00489 -0.0658 -0.0221 -0.0451
## # … with 53 more variables: rz_opn <dbl>, mzextra <dbl>, mzagree <dbl>,
## # mzconsc <dbl>, mzneuro <dbl>, mzopen <dbl>, fzextra <dbl>, fzagree <dbl>,
## # fzconsc <dbl>, fzneuro <dbl>, fzopen <dbl>, E_LT30 <dbl>, A_LT30 <dbl>,
## # C_LT30 <dbl>, N_LT30 <dbl>, O_LT30 <dbl>, E_GT30 <dbl>, A_GT30 <dbl>,
## # C_GT30 <dbl>, N_GT30 <dbl>, O_GT30 <dbl>, GenD_E <dbl>, GenD_A <dbl>,
## # GenD_C <dbl>, GenD_N <dbl>, GenD_O <dbl>, AgeD_E <dbl>, AgeD_A <dbl>,
## # AgeD_C <dbl>, AgeD_N <dbl>, AgeD_O <dbl>, TFR <dbl>, alpha <dbl>, …
junkins <- as.data.frame(junkins)
head(junkins)
## STATE_ABBR statename state region division rz_ext rz_agr rz_cns
## 1 AK Alabama 1 3 6 -0.125762 -0.104588 -0.109966
## 2 AL Alaska 2 4 9 0.043042 0.100483 0.080650
## 3 AR Arizona 4 4 8 -0.009335 -0.015433 -0.058324
## 4 AZ Arkansas 5 3 7 0.012155 -0.007023 0.008430
## 5 CA California 6 4 9 -0.043034 -0.035667 -0.065377
## 6 CO Colorado 8 4 8 -0.004892 -0.065783 -0.022110
## rz_neu rz_opn mzextra mzagree mzconsc mzneuro mzopen fzextra
## 1 -0.049075 0.051630 -0.004242 0.412499 0.301710 -0.326062 0.427654 0.139469
## 2 -0.015238 -0.112345 0.112153 0.504055 0.357393 -0.292434 0.394611 0.250319
## 3 0.076501 -0.080592 0.090463 0.468918 0.313488 -0.242351 0.435278 0.200209
## 4 -0.043650 0.002879 0.100366 0.469782 0.343585 -0.307043 0.452290 0.219097
## 5 -0.014349 0.092209 0.058111 0.463655 0.294992 -0.271535 0.496399 0.187948
## 6 -0.045139 0.073840 0.084083 0.433223 0.310563 -0.315733 0.477640 0.211284
## fzagree fzconsc fzneuro fzopen E_LT30 A_LT30 C_LT30 N_LT30
## 1 0.534365 0.337558 -0.013555 0.419930 -0.140225 -0.133313 -0.142583 -0.041884
## 2 0.635946 0.440848 0.012200 0.300435 0.051194 0.140257 0.130334 -0.030951
## 3 0.574798 0.384941 0.061924 0.311068 -0.009714 -0.012163 -0.048157 0.072481
## 4 0.578078 0.413102 -0.013868 0.365238 -0.003936 0.007324 0.020526 -0.047188
## 5 0.557243 0.376993 -0.004257 0.411690 -0.070760 -0.023765 -0.059610 -0.013238
## 6 0.552301 0.406930 -0.010579 0.407388 -0.007388 -0.066719 -0.012239 -0.044091
## O_LT30 E_GT30 A_GT30 C_GT30 N_GT30 O_GT30 GenD_E
## 1 0.064560 -0.093362 -0.040240 -0.036899 -0.065184 0.022665 -0.143711
## 2 -0.109746 0.017092 -0.026130 -0.077508 0.034782 -0.120618 -0.138166
## 3 -0.036030 -0.008526 -0.022415 -0.080035 0.085087 -0.175739 -0.109746
## 4 0.014119 0.051829 -0.042397 -0.021394 -0.034928 -0.024835 -0.118731
## 5 0.082416 0.027033 -0.065745 -0.079949 -0.017157 0.116955 -0.129837
## 6 0.078307 0.001056 -0.063551 -0.045642 -0.047637 0.063192 -0.127201
## GenD_A GenD_C GenD_N GenD_O AgeD_E AgeD_A AgeD_C
## 1 -0.121866 -0.035848 -0.312507 0.007724 -0.046863 -0.093073 -0.105684
## 2 -0.131891 -0.083455 -0.304634 0.094176 0.034101 0.166388 0.207842
## 3 -0.105880 -0.071453 -0.304275 0.124210 -0.001189 0.010252 0.031878
## 4 -0.108296 -0.069517 -0.293175 0.087052 -0.055765 0.049721 0.041920
## 5 -0.093588 -0.082001 -0.267278 0.084709 -0.097794 0.041980 0.020339
## 6 -0.119078 -0.096367 -0.305154 0.070252 -0.008443 -0.003168 0.033403
## AgeD_N AgeD_O TFR alpha peak stop ageFB t_ageFM nevermar
## 1 0.023300 0.041896 2.3470 13.338400 23.90560 2.854100 24.3 25.85 0.316093
## 2 -0.065733 0.010872 1.8715 11.279618 24.63356 4.435501 23.6 26.60 0.291652
## 3 -0.012606 0.139709 2.0030 12.428397 23.17632 4.598583 23.0 25.65 0.263799
## 4 -0.012260 0.038953 2.0680 10.913909 25.19252 2.924860 24.0 26.80 0.316186
## 5 0.003918 -0.034539 1.9475 7.012688 28.85349 2.941833 25.6 28.30 0.360036
## 6 0.003546 0.015115 1.9240 6.950724 28.27968 3.288943 25.7 27.15 0.306618
## divorce cohabit abortion t_nmf unintprg famplnpw med_inc perAA perHisp
## 1 0.016276 8.2 12.0 31.9 53 147 64576 3.7 5.5
## 2 0.017909 4.8 12.0 45.0 55 147 40474 26.2 3.9
## 3 0.018978 5.3 8.7 39.1 56 152 38307 15.4 6.4
## 4 0.014391 7.7 15.2 41.3 51 151 46789 4.1 29.6
## 5 0.012347 8.0 27.6 33.9 56 245 57708 6.2 37.6
## 6 0.015716 8.1 15.7 29.8 48 80 54046 4.0 20.7
## perFem perBA perUrb voteO vryrel relcons
## 1 47.9 27.2 66.02 38.74 56.3 42.76
## 2 51.5 21.7 59.04 37.89 27.9 18.75
## 3 50.9 19.1 56.16 45.12 35.7 18.08
## 4 50.3 26.3 89.81 38.86 52.1 39.93
## 5 50.3 30.1 94.95 61.01 34.0 11.45
## 6 49.9 35.9 86.15 53.66 32.6 14.78
summary(junkins)
## STATE_ABBR statename state region
## Length:50 Length:50 Min. : 1.00 Length:50
## Class :character Class :character 1st Qu.:17.25 Class :character
## Mode :character Mode :character Median :29.50 Mode :character
## Mean :29.32
## 3rd Qu.:41.75
## Max. :56.00
## division rz_ext rz_agr
## Length:50 Min. :-0.125762 Min. :-0.1514890
## Class :character 1st Qu.:-0.020396 1st Qu.:-0.0269132
## Mode :character Median : 0.001697 Median : 0.0000335
## Mean :-0.001259 Mean :-0.0002911
## 3rd Qu.: 0.020437 3rd Qu.: 0.0318240
## Max. : 0.080370 Max. : 0.1141520
## rz_cns rz_neu rz_opn
## Min. :-0.1099660 Min. :-0.079314 Min. :-0.16522
## 1st Qu.:-0.0325760 1st Qu.:-0.037471 1st Qu.:-0.06737
## Median : 0.0045515 Median :-0.006724 Median :-0.02154
## Mean :-0.0002793 Mean : 0.004376 Mean :-0.01949
## 3rd Qu.: 0.0371368 3rd Qu.: 0.047362 3rd Qu.: 0.03460
## Max. : 0.0919760 Max. : 0.136903 Max. : 0.09221
## mzextra mzagree mzconsc mzneuro
## Min. :-0.004242 Min. :0.3932 Min. :0.2625 Min. :-0.3382
## 1st Qu.: 0.072906 1st Qu.:0.4625 1st Qu.:0.3019 1st Qu.:-0.3107
## Median : 0.092419 Median :0.4729 Median :0.3249 Median :-0.2883
## Mean : 0.091451 Mean :0.4724 Mean :0.3237 Mean :-0.2877
## 3rd Qu.: 0.108753 3rd Qu.:0.4863 3rd Qu.:0.3476 3rd Qu.:-0.2689
## Max. : 0.174489 Max. :0.5227 Max. :0.3818 Max. :-0.2161
## mzopen fzextra fzagree fzconsc
## Min. :0.3693 Min. :0.1395 Min. :0.4985 Min. :0.3376
## 1st Qu.:0.4078 1st Qu.:0.1995 1st Qu.:0.5638 1st Qu.:0.3921
## Median :0.4342 Median :0.2117 Median :0.5799 Median :0.4096
## Mean :0.4330 Mean :0.2135 Mean :0.5791 Mean :0.4072
## 3rd Qu.:0.4541 3rd Qu.:0.2278 3rd Qu.:0.5977 3rd Qu.:0.4274
## Max. :0.5111 Max. :0.2646 Max. :0.6412 Max. :0.4562
## fzneuro fzopen E_LT30 A_LT30
## Min. :-0.044549 Min. :0.2653 Min. :-0.140225 Min. :-0.163454
## 1st Qu.:-0.004742 1st Qu.:0.3288 1st Qu.:-0.031605 1st Qu.:-0.019572
## Median : 0.017247 Median :0.3511 Median :-0.004358 Median : 0.007515
## Mean : 0.023099 Mean :0.3560 Mean :-0.006035 Mean : 0.008914
## 3rd Qu.: 0.051673 3rd Qu.:0.3837 3rd Qu.: 0.022609 3rd Qu.: 0.037691
## Max. : 0.116392 Max. :0.4225 Max. : 0.088809 Max. : 0.148942
## C_LT30 N_LT30 O_LT30 E_GT30
## Min. :-0.14258 Min. :-0.0912820 Min. :-0.16738 Min. :-0.093362
## 1st Qu.:-0.01245 1st Qu.:-0.0433178 1st Qu.:-0.04830 1st Qu.:-0.008276
## Median : 0.01680 Median :-0.0069445 Median :-0.01515 Median : 0.010092
## Mean : 0.01752 Mean : 0.0004047 Mean :-0.01565 Mean : 0.009377
## 3rd Qu.: 0.06213 3rd Qu.: 0.0400875 3rd Qu.: 0.02512 3rd Qu.: 0.031078
## Max. : 0.13033 Max. : 0.1244140 Max. : 0.10773 Max. : 0.065151
## A_GT30 C_GT30 N_GT30 O_GT30
## Min. :-0.138535 Min. :-0.13851 Min. :-0.12421 Min. :-0.18060
## 1st Qu.:-0.048372 1st Qu.:-0.07934 1st Qu.:-0.02920 1st Qu.:-0.08721
## Median :-0.028375 Median :-0.04648 Median : 0.01158 Median :-0.03954
## Mean :-0.027503 Mean :-0.04923 Mean : 0.01610 Mean :-0.02667
## 3rd Qu.:-0.004039 3rd Qu.:-0.03023 3rd Qu.: 0.05947 3rd Qu.: 0.02664
## Max. : 0.062225 Max. : 0.02385 Max. : 0.16735 Max. : 0.12963
## GenD_E GenD_A GenD_C GenD_N
## Min. :-0.17148 Min. :-0.13715 Min. :-0.14416 Min. :-0.3513
## 1st Qu.:-0.13085 1st Qu.:-0.11716 1st Qu.:-0.09846 1st Qu.:-0.3216
## Median :-0.12175 Median :-0.10752 Median :-0.08502 Median :-0.3111
## Mean :-0.12208 Mean :-0.10676 Mean :-0.08345 Mean :-0.3108
## 3rd Qu.:-0.10979 3rd Qu.:-0.09424 3rd Qu.:-0.06902 3rd Qu.:-0.3036
## Max. :-0.08912 Max. :-0.07073 Max. :-0.03207 Max. :-0.2272
## GenD_O AgeD_E AgeD_A AgeD_C
## Min. :0.007724 Min. :-0.10067 Min. :-0.093073 Min. :-0.10568
## 1st Qu.:0.065139 1st Qu.:-0.04403 1st Qu.: 0.008226 1st Qu.: 0.03346
## Median :0.078479 Median :-0.01192 Median : 0.038546 Median : 0.06595
## Mean :0.077021 Mean :-0.01541 Mean : 0.036417 Mean : 0.06675
## 3rd Qu.:0.093456 3rd Qu.: 0.01037 3rd Qu.: 0.049215 3rd Qu.: 0.10542
## Max. :0.137533 Max. : 0.05031 Max. : 0.166388 Max. : 0.20999
## AgeD_N AgeD_O TFR alpha
## Min. :-0.111473 Min. :-0.10765 Min. :1.628 Min. : 6.059
## 1st Qu.:-0.041758 1st Qu.:-0.03875 1st Qu.:1.874 1st Qu.: 9.697
## Median :-0.012433 Median : 0.01363 Median :1.939 Median :10.766
## Mean :-0.015698 Mean : 0.01101 Mean :1.949 Mean :10.510
## 3rd Qu.: 0.008862 3rd Qu.: 0.05730 3rd Qu.:2.030 3rd Qu.:11.833
## Max. : 0.090267 Max. : 0.13971 Max. :2.449 Max. :13.338
## peak stop ageFB t_ageFM
## Min. :22.72 Min. :1.282 Min. :22.60 Min. :24.45
## 1st Qu.:24.64 1st Qu.:3.332 1st Qu.:24.00 1st Qu.:26.65
## Median :26.80 Median :4.021 Median :24.65 Median :27.32
## Mean :26.83 Mean :3.872 Mean :24.84 Mean :27.31
## 3rd Qu.:28.66 3rd Qu.:4.511 3rd Qu.:25.68 3rd Qu.:28.10
## Max. :32.16 Max. :5.164 Max. :27.70 Max. :29.75
## nevermar divorce cohabit abortion
## Min. :0.2490 Min. :0.009969 Min. :4.600 Min. : 0.90
## 1st Qu.:0.2861 1st Qu.:0.013130 1st Qu.:6.125 1st Qu.: 9.00
## Median :0.3096 Median :0.014384 Median :6.550 Median :15.35
## Mean :0.3070 Mean :0.014600 Mean :6.878 Mean :15.62
## 3rd Qu.:0.3255 3rd Qu.:0.015841 3rd Qu.:7.675 3rd Qu.:19.00
## Max. :0.3753 Max. :0.019180 Max. :9.300 Max. :40.00
## t_nmf unintprg famplnpw med_inc
## Min. :15.80 Min. :38.00 Min. : 31.00 Min. :36851
## 1st Qu.:31.82 1st Qu.:48.00 1st Qu.: 73.25 1st Qu.:43693
## Median :35.85 Median :52.50 Median :106.00 Median :48333
## Mean :35.55 Mean :51.56 Mean :107.46 Mean :49755
## 3rd Qu.:39.48 3rd Qu.:56.00 3rd Qu.:142.00 3rd Qu.:54595
## Max. :51.20 Max. :65.00 Max. :245.00 Max. :68854
## perAA perHisp perFem perBA
## Min. : 0.400 Min. : 1.20 Min. :47.90 Min. :17.30
## 1st Qu.: 3.025 1st Qu.: 4.25 1st Qu.:50.33 1st Qu.:24.15
## Median : 7.000 Median : 8.20 Median :50.75 Median :26.35
## Mean :10.338 Mean :10.61 Mean :50.66 Mean :27.16
## 3rd Qu.:15.175 3rd Qu.:12.22 3rd Qu.:51.27 3rd Qu.:30.25
## Max. :37.000 Max. :46.30 Max. :51.70 Max. :38.30
## perUrb voteO vryrel relcons
## Min. :38.66 Min. :32.54 Min. :23.80 Min. : 2.85
## 1st Qu.:65.08 1st Qu.:42.69 1st Qu.:33.17 1st Qu.:12.89
## Median :73.73 Median :51.27 Median :39.05 Median :15.95
## Mean :73.58 Mean :50.51 Mean :39.62 Mean :19.55
## 3rd Qu.:86.94 3rd Qu.:57.39 3rd Qu.:45.70 3rd Qu.:25.87
## Max. :94.95 Max. :71.85 Max. :56.60 Max. :71.40
junkins.pc <- prcomp(junkins[,c("fzextra","fzagree","fzconsc","fzneuro","fzopen", "ageFB","nevermar","peak")], center = TRUE,scale. = TRUE,retx=T,rownames=junkins[,1])
## Warning: In prcomp.default(junkins[, c("fzextra", "fzagree", "fzconsc", "fzneuro",
## "fzopen", "ageFB", "nevermar", "peak")], center = TRUE, scale. = TRUE,
## retx = T, rownames = junkins[, 1]) :
## extra argument 'rownames' will be disregarded
summary(junkins.pc)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 1.8522 1.3791 1.0819 0.77005 0.62075 0.55748 0.35387
## Proportion of Variance 0.4288 0.2377 0.1463 0.07412 0.04817 0.03885 0.01565
## Cumulative Proportion 0.4288 0.6666 0.8129 0.88701 0.93517 0.97402 0.98968
## PC8
## Standard deviation 0.28739
## Proportion of Variance 0.01032
## Cumulative Proportion 1.00000
junkins.pc$rotation
## PC1 PC2 PC3 PC4 PC5
## fzextra 0.2613991 -0.49624652 -0.336685867 0.0454904 -0.373449032
## fzagree 0.4209046 -0.32386904 0.159537182 0.1621577 0.265360354
## fzconsc 0.4226129 -0.25282973 0.194011891 -0.1343432 0.616800567
## fzneuro -0.1668324 0.07018339 -0.808066109 0.2364757 0.501018047
## fzopen -0.4549172 0.09585578 0.306806718 -0.1210681 0.353860251
## ageFB -0.3928770 -0.42551129 0.006225275 -0.3030353 0.154822035
## nevermar -0.3032565 -0.31128290 0.263294645 0.8343062 -0.002282729
## peak -0.3079100 -0.54116026 -0.084177653 -0.3084023 -0.097290951
## PC6 PC7 PC8
## fzextra 0.54942404 -0.34650335 -0.09266937
## fzagree -0.51099376 -0.51767316 0.25823373
## fzconsc 0.36461292 0.41871425 -0.11436596
## fzneuro -0.03547813 -0.02603067 0.07344583
## fzopen 0.46211326 -0.51461617 0.26735488
## ageFB -0.26650238 -0.14130673 -0.67657797
## nevermar 0.06452209 0.19305302 -0.06564894
## peak -0.11866779 0.33746680 0.61049791
biplot(junkins.pc, scale = 0,xlabs=junkins[,1])
### Scree Plot
screeplot(junkins.pc, type = "l", main = "Scree Plot")
abline(h=1)
junkins.play <- PCA(junkins[,c("fzextra","fzagree","fzconsc","fzneuro","fzopen", "ageFB","nevermar","peak")], scale.unit=T,graph=F)
## Error in PCA(junkins[, c("fzextra", "fzagree", "fzconsc", "fzneuro", "fzopen", : could not find function "PCA"
eigenvalues<-junkins.play$eig
## Error in eval(expr, envir, enclos): object 'junkins.play' not found
head(eigenvalues[,1:2])
## Error in head(eigenvalues[, 1:2]): object 'eigenvalues' not found
hist(junkins.pc$x[,1])
hist(junkins.pc$x[,2])
hist(junkins.pc$x[,3])
### Correlation
cor(junkins.pc$x[,1],junkins.pc$x[,2])
## [1] -1.179801e-18
cor(junkins.pc$x[,1],junkins.pc$x[,3])
## [1] 4.35369e-16
cor(junkins.pc$x[,2],junkins.pc$x[,3])
## [1] 2.618658e-15
scores<-data.frame(junkins.pc$x)
junkins<-cbind(junkins,scores)
ggplot(junkins, aes(PC1, PC2, col = Species, fill = region)) +
stat_ellipse(geom = "polygon", col = "black", alpha = 0.5) +
geom_point(shape = 21, col = "black")
### Correlations of original variables with PC scores
round(cor(junkins[,c("fzextra","fzagree","fzconsc","fzneuro","fzopen", "ageFB","nevermar","peak","PC1","PC2","PC3")]), 3)
## fzextra fzagree fzconsc fzneuro fzopen ageFB nevermar peak PC1
## fzextra 1.000 0.520 0.494 0.031 -0.574 -0.018 -0.056 0.234 0.484
## fzagree 0.520 1.000 0.765 -0.352 -0.668 -0.280 -0.141 -0.157 0.780
## fzconsc 0.494 0.765 1.000 -0.365 -0.519 -0.334 -0.279 -0.205 0.783
## fzneuro 0.031 -0.352 -0.365 1.000 0.032 0.149 -0.002 0.125 -0.309
## fzopen -0.574 -0.668 -0.519 0.032 1.000 0.537 0.446 0.335 -0.843
## ageFB -0.018 -0.280 -0.334 0.149 0.537 1.000 0.507 0.872 -0.728
## nevermar -0.056 -0.141 -0.279 -0.002 0.446 0.507 1.000 0.465 -0.562
## peak 0.234 -0.157 -0.205 0.125 0.335 0.872 0.465 1.000 -0.570
## PC1 0.484 0.780 0.783 -0.309 -0.843 -0.728 -0.562 -0.570 1.000
## PC2 -0.684 -0.447 -0.349 0.097 0.132 -0.587 -0.429 -0.746 0.000
## PC3 -0.364 0.173 0.210 -0.874 0.332 0.007 0.285 -0.091 0.000
## PC2 PC3
## fzextra -0.684 -0.364
## fzagree -0.447 0.173
## fzconsc -0.349 0.210
## fzneuro 0.097 -0.874
## fzopen 0.132 0.332
## ageFB -0.587 0.007
## nevermar -0.429 0.285
## peak -0.746 -0.091
## PC1 0.000 0.000
## PC2 1.000 0.000
## PC3 0.000 1.000