install.packages("https://cran.rstudio.com/bin/windows/contrib/4.1/faraway_1.0.7.zip", repos = F)
## 將程式套件安載入 'C:/Users/user/Documents/R/win-library/4.1'
## (因為 'lib' 沒有被指定)
## Warning: unable to access index for repository FALSE/src/contrib:
## 網址 'FALSE/src/contrib/PACKAGES' 不支援方案格式
## Warning: package 'https://cran.rstudio.com/bin/windows/contrib/4.1/faraway_1.0.7.zip' is not available for this version of R
##
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository FALSE/bin/windows/contrib/4.1:
## 網址 'FALSE/bin/windows/contrib/4.1/PACKAGES' 不支援方案格式
library(faraway)
## Warning: 套件 'faraway' 是用 R 版本 4.1.3 來建造的
library(lattice)
##
## 載入套件:'lattice'
## 下列物件被遮斷自 'package:faraway':
##
## melanoma
決定使用fortune這一筆資料
data("fortune")
檢視六筆資料
head(fortune)
## wealth age region
## 1 37.0 50 M
## 2 24.0 88 U
## 3 14.0 64 A
## 4 13.0 63 U
## 5 13.0 66 U
## 6 11.7 72 E
檢視資料結構
str(fortune)
## 'data.frame': 232 obs. of 3 variables:
## $ wealth: num 37 24 14 13 13 11.7 10 8.2 8.1 7.2 ...
## $ age : int 50 88 64 63 66 72 71 77 68 66 ...
## $ region: Factor w/ 5 levels "A","E","M","O",..: 3 5 1 5 5 2 3 5 5 2 ...
看看每個區域富豪的情況
table(fortune$region)
##
## A E M O U
## 38 80 22 29 63
檢視財富的狀況
summary(fortune$wealth)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.300 1.800 2.684 3.000 37.000
各區域中財富的平均數
aggregate(wealth ~ region, data = fortune, FUN = mean)
## region wealth
## 1 A 2.621053
## 2 E 2.207500
## 3 M 4.263636
## 4 O 2.234483
## 5 U 2.984127
各區域中財富的標準差
aggregate(wealth ~ region, data = fortune, FUN = sd)
## region wealth
## 1 A 2.170833
## 2 E 1.597686
## 3 M 7.657150
## 4 O 1.264989
## 5 U 3.632524
畫圖看地區與財富間關係
densityplot(~ wealth, groups = region, data = fortune, xlab = 'Wealth', lty = c(1,2,3,4,5),
plot.points = F, type = "g")
畫boxplot圖看地區與財富間關係
boxplot(wealth ~ region, fortune, col = "blue", border = "black")
畫圖看不同區域財富的資料分數直方圖
histogram(~ wealth | region, data = fortune, xlab = 'Wealth', ylab='機率',
type = 'density', layout = c(5, 1))
boxplot(wealth ~ region, fortune, xlab = "Region", ylab = "Wealth", frame = F, col = c("#00AFBB", "#E7B800", "#FC4E07", "#0000FF"))
不同區域間財富的平均數標準誤
aggregate(wealth ~ region, data = fortune, function(x) sd(x)/sqrt(length(x)))
## region wealth
## 1 A 0.3521557
## 2 E 0.1786268
## 3 M 1.6325099
## 4 O 0.2349026
## 5 U 0.4576550
不同區域間年齡的平均標準誤
aggregate(age ~ region, data = fortune, function(x) sd(x)/sqrt(length(x)))
## region age
## 1 A 1.637225
## 2 E 1.685748
## 3 M 4.163297
## 4 O 2.471913
## 5 U 1.508630
年齡與財富散佈圖
plot(fortune$wealth, fortune$age, col=blues9, pch = 16, xlab = "Wealth", ylab = "Age")
#看不同區域間,財富與年齡間的關係是否類似
xyplot(age ~ wealth | region, data = fortune, xlab = 'Wealth', ylab = 'Age',
type = c("g", "p", "r"), cex = 0.1, layout = c(5, 1))
資料結果說明不同區域間皆有貧富差距(看Boxplot圖便可知,形狀很扁)