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("fat")
str(fat)
## 'data.frame': 252 obs. of 18 variables:
## $ brozek : num 12.6 6.9 24.6 10.9 27.8 20.6 19 12.8 5.1 12 ...
## $ siri : num 12.3 6.1 25.3 10.4 28.7 20.9 19.2 12.4 4.1 11.7 ...
## $ density: num 1.07 1.09 1.04 1.08 1.03 ...
## $ age : int 23 22 22 26 24 24 26 25 25 23 ...
## $ weight : num 154 173 154 185 184 ...
## $ height : num 67.8 72.2 66.2 72.2 71.2 ...
## $ adipos : num 23.7 23.4 24.7 24.9 25.6 26.5 26.2 23.6 24.6 25.8 ...
## $ free : num 135 161 116 165 133 ...
## $ neck : num 36.2 38.5 34 37.4 34.4 39 36.4 37.8 38.1 42.1 ...
## $ chest : num 93.1 93.6 95.8 101.8 97.3 ...
## $ abdom : num 85.2 83 87.9 86.4 100 94.4 90.7 88.5 82.5 88.6 ...
## $ hip : num 94.5 98.7 99.2 101.2 101.9 ...
## $ thigh : num 59 58.7 59.6 60.1 63.2 66 58.4 60 62.9 63.1 ...
## $ knee : num 37.3 37.3 38.9 37.3 42.2 42 38.3 39.4 38.3 41.7 ...
## $ ankle : num 21.9 23.4 24 22.8 24 25.6 22.9 23.2 23.8 25 ...
## $ biceps : num 32 30.5 28.8 32.4 32.2 35.7 31.9 30.5 35.9 35.6 ...
## $ forearm: num 27.4 28.9 25.2 29.4 27.7 30.6 27.8 29 31.1 30 ...
## $ wrist : num 17.1 18.2 16.6 18.2 17.7 18.8 17.7 18.8 18.2 19.2 ...
head(fat)
## brozek siri density age weight height adipos free neck chest abdom hip
## 1 12.6 12.3 1.0708 23 154.25 67.75 23.7 134.9 36.2 93.1 85.2 94.5
## 2 6.9 6.1 1.0853 22 173.25 72.25 23.4 161.3 38.5 93.6 83.0 98.7
## 3 24.6 25.3 1.0414 22 154.00 66.25 24.7 116.0 34.0 95.8 87.9 99.2
## 4 10.9 10.4 1.0751 26 184.75 72.25 24.9 164.7 37.4 101.8 86.4 101.2
## 5 27.8 28.7 1.0340 24 184.25 71.25 25.6 133.1 34.4 97.3 100.0 101.9
## 6 20.6 20.9 1.0502 24 210.25 74.75 26.5 167.0 39.0 104.5 94.4 107.8
## thigh knee ankle biceps forearm wrist
## 1 59.0 37.3 21.9 32.0 27.4 17.1
## 2 58.7 37.3 23.4 30.5 28.9 18.2
## 3 59.6 38.9 24.0 28.8 25.2 16.6
## 4 60.1 37.3 22.8 32.4 29.4 18.2
## 5 63.2 42.2 24.0 32.2 27.7 17.7
## 6 66.0 42.0 25.6 35.7 30.6 18.8
summary(fat)
## brozek siri density age
## Min. : 0.00 Min. : 0.00 Min. :0.995 Min. :22.00
## 1st Qu.:12.80 1st Qu.:12.47 1st Qu.:1.041 1st Qu.:35.75
## Median :19.00 Median :19.20 Median :1.055 Median :43.00
## Mean :18.94 Mean :19.15 Mean :1.056 Mean :44.88
## 3rd Qu.:24.60 3rd Qu.:25.30 3rd Qu.:1.070 3rd Qu.:54.00
## Max. :45.10 Max. :47.50 Max. :1.109 Max. :81.00
## weight height adipos free
## Min. :118.5 Min. :29.50 Min. :18.10 Min. :105.9
## 1st Qu.:159.0 1st Qu.:68.25 1st Qu.:23.10 1st Qu.:131.3
## Median :176.5 Median :70.00 Median :25.05 Median :141.6
## Mean :178.9 Mean :70.15 Mean :25.44 Mean :143.7
## 3rd Qu.:197.0 3rd Qu.:72.25 3rd Qu.:27.32 3rd Qu.:153.9
## Max. :363.1 Max. :77.75 Max. :48.90 Max. :240.5
## neck chest abdom hip
## Min. :31.10 Min. : 79.30 Min. : 69.40 Min. : 85.0
## 1st Qu.:36.40 1st Qu.: 94.35 1st Qu.: 84.58 1st Qu.: 95.5
## Median :38.00 Median : 99.65 Median : 90.95 Median : 99.3
## Mean :37.99 Mean :100.82 Mean : 92.56 Mean : 99.9
## 3rd Qu.:39.42 3rd Qu.:105.38 3rd Qu.: 99.33 3rd Qu.:103.5
## Max. :51.20 Max. :136.20 Max. :148.10 Max. :147.7
## thigh knee ankle biceps forearm
## Min. :47.20 Min. :33.00 Min. :19.1 Min. :24.80 Min. :21.00
## 1st Qu.:56.00 1st Qu.:36.98 1st Qu.:22.0 1st Qu.:30.20 1st Qu.:27.30
## Median :59.00 Median :38.50 Median :22.8 Median :32.05 Median :28.70
## Mean :59.41 Mean :38.59 Mean :23.1 Mean :32.27 Mean :28.66
## 3rd Qu.:62.35 3rd Qu.:39.92 3rd Qu.:24.0 3rd Qu.:34.33 3rd Qu.:30.00
## Max. :87.30 Max. :49.10 Max. :33.9 Max. :45.00 Max. :34.90
## wrist
## Min. :15.80
## 1st Qu.:17.60
## Median :18.30
## Mean :18.23
## 3rd Qu.:18.80
## Max. :21.40
#散布圖
plot(fat$age, fat$weight)

#直方圖
hist(fat$age)

library(lattice)
##
## 載入套件:'lattice'
## 下列物件被遮斷自 'package:faraway':
##
## melanoma
histogram(~ age, data = fat, xlab = '年紀', ylab = '機率', type = "density")

#連續變項間關係
dta_scores <- fat[, c('age', 'weight', 'height')]
#兩兩變項畫散佈圖
pairs(dta_scores, pch = '.', upper.panel = panel.smooth, lower.panel = NULL,
col = 'brown')

#age和height相關
round(cor(fat$age,fat$height), 3)
## [1] -0.172
#所有相關
round(cor(dta_scores), 3)
## age weight height
## age 1.000 -0.013 -0.172
## weight -0.013 1.000 0.308
## height -0.172 0.308 1.000
#檢定相關是否顯著,也可以看到信賴區間
cor.test( ~ age + height , data = dta_scores)
##
## Pearson's product-moment correlation
##
## data: age and height
## t = -2.7548, df = 250, p-value = 0.006304
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.28908627 -0.04911403
## sample estimates:
## cor
## -0.1716451
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(dta_scores), type="pearson")
## age weight height
## age 1.00 -0.01 -0.17
## weight -0.01 1.00 0.31
## height -0.17 0.31 1.00
##
## n= 252
##
##
## P
## age weight height
## age 0.8404 0.0063
## weight 0.8404 0.0000
## height 0.0063 0.0000