install.packages("https://cran.rstudio.com/bin/windows/contrib/4.1/faraway_1.0.7.zip", repos = NULL)
## 將程式套件安載入 'C:/Users/user/Documents/R/win-library/4.1'
## (因為 'lib' 沒有被指定)
## package 'faraway' successfully unpacked and MD5 sums checked
library(faraway)
## Warning: 套件 'faraway' 是用 R 版本 4.1.2 來建造的
data("divusa")
head(divusa)
## year divorce unemployed femlab marriage birth military
## 1 1920 8.0 5.2 22.70 92.0 117.9 3.2247
## 2 1921 7.2 11.7 22.79 83.0 119.8 3.5614
## 3 1922 6.6 6.7 22.88 79.7 111.2 2.4553
## 4 1923 7.1 2.4 22.97 85.2 110.5 2.2065
## 5 1924 7.2 5.0 23.06 80.3 110.9 2.2889
## 6 1925 7.2 3.2 23.15 79.2 106.6 2.1735
str(divusa)
## 'data.frame': 77 obs. of 7 variables:
## $ year : int 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 ...
## $ divorce : num 8 7.2 6.6 7.1 7.2 7.2 7.5 7.8 7.8 8 ...
## $ unemployed: num 5.2 11.7 6.7 2.4 5 3.2 1.8 3.3 4.2 3.2 ...
## $ femlab : num 22.7 22.8 22.9 23 23.1 ...
## $ marriage : num 92 83 79.7 85.2 80.3 79.2 78.7 77 74.1 75.5 ...
## $ birth : num 118 120 111 110 111 ...
## $ military : num 3.22 3.56 2.46 2.21 2.29 ...
summary(divusa)
## year divorce unemployed femlab
## Min. :1920 Min. : 6.10 Min. : 1.200 Min. :22.70
## 1st Qu.:1939 1st Qu.: 8.70 1st Qu.: 4.200 1st Qu.:27.47
## Median :1958 Median :10.60 Median : 5.600 Median :37.10
## Mean :1958 Mean :13.27 Mean : 7.173 Mean :38.58
## 3rd Qu.:1977 3rd Qu.:20.30 3rd Qu.: 7.500 3rd Qu.:47.80
## Max. :1996 Max. :22.80 Max. :24.900 Max. :59.30
## marriage birth military
## Min. : 49.70 Min. : 65.30 Min. : 1.940
## 1st Qu.: 61.90 1st Qu.: 68.90 1st Qu.: 3.469
## Median : 74.10 Median : 85.90 Median : 9.102
## Mean : 72.97 Mean : 88.89 Mean :12.365
## 3rd Qu.: 80.00 3rd Qu.:107.30 3rd Qu.:14.266
## Max. :118.10 Max. :122.90 Max. :86.641
plot(divusa$divorce, divusa$unemployed)

hist(divusa$divorce)

library(lattice)
##
## 載入套件:'lattice'
## 下列物件被遮斷自 'package:faraway':
##
## melanoma
histogram(~ divorce, data = divusa, xlab = 'year', ylab='rate',type = "density")

divusa_scores <- divusa[, c('divorce', 'unemployed', 'femlab', 'marriage', 'birth', 'military')]
pairs(divusa_scores, pch = '.', upper.panel = panel.smooth, lower.panel = NULL,
col = 'red')

round(cor(divusa$divorce,divusa$unemployed), 3)
## [1] -0.211
round(cor(divusa_scores), 3)
## divorce unemployed femlab marriage birth military
## divorce 1.000 -0.211 0.910 -0.534 -0.722 0.019
## unemployed -0.211 1.000 -0.257 -0.271 -0.314 -0.400
## femlab 0.910 -0.257 1.000 -0.649 -0.604 0.051
## marriage -0.534 -0.271 -0.649 1.000 0.674 0.258
## birth -0.722 -0.314 -0.604 0.674 1.000 0.141
## military 0.019 -0.400 0.051 0.258 0.141 1.000
cor.test( ~ divorce + unemployed, data = divusa_scores)
##
## Pearson's product-moment correlation
##
## data: divorce and unemployed
## t = -1.8657, df = 75, p-value = 0.06599
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.41500478 0.01403919
## sample estimates:
## cor
## -0.2106019
library(Hmisc)
## Warning: 套件 'Hmisc' 是用 R 版本 4.1.2 來建造的
## 載入需要的套件:survival
##
## 載入套件:'survival'
## 下列物件被遮斷自 'package:faraway':
##
## rats, solder
## 載入需要的套件:Formula
## Warning: 套件 'Formula' 是用 R 版本 4.1.1 來建造的
## 載入需要的套件:ggplot2
## Warning: 套件 'ggplot2' 是用 R 版本 4.1.2 來建造的
##
## 載入套件:'Hmisc'
## 下列物件被遮斷自 'package:base':
##
## format.pval, units
rcorr(as.matrix(divusa_scores), type="pearson")
## divorce unemployed femlab marriage birth military
## divorce 1.00 -0.21 0.91 -0.53 -0.72 0.02
## unemployed -0.21 1.00 -0.26 -0.27 -0.31 -0.40
## femlab 0.91 -0.26 1.00 -0.65 -0.60 0.05
## marriage -0.53 -0.27 -0.65 1.00 0.67 0.26
## birth -0.72 -0.31 -0.60 0.67 1.00 0.14
## military 0.02 -0.40 0.05 0.26 0.14 1.00
##
## n= 77
##
##
## P
## divorce unemployed femlab marriage birth military
## divorce 0.0660 0.0000 0.0000 0.0000 0.8726
## unemployed 0.0660 0.0238 0.0172 0.0054 0.0003
## femlab 0.0000 0.0238 0.0000 0.0000 0.6579
## marriage 0.0000 0.0172 0.0000 0.0000 0.0234
## birth 0.0000 0.0054 0.0000 0.0000 0.2216
## military 0.8726 0.0003 0.6579 0.0234 0.2216