```r
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('hsb')
summary(hsb)
## id gender race ses schtyp
## Min. : 1.00 female:109 african-amer: 20 high :58 private: 32
## 1st Qu.: 50.75 male : 91 asian : 11 low :47 public :168
## Median :100.50 hispanic : 24 middle:95
## Mean :100.50 white :145
## 3rd Qu.:150.25
## Max. :200.00
## prog read write math science
## academic:105 Min. :28.00 Min. :31.00 Min. :33.00 Min. :26.00
## general : 45 1st Qu.:44.00 1st Qu.:45.75 1st Qu.:45.00 1st Qu.:44.00
## vocation: 50 Median :50.00 Median :54.00 Median :52.00 Median :53.00
## Mean :52.23 Mean :52.77 Mean :52.65 Mean :51.85
## 3rd Qu.:60.00 3rd Qu.:60.00 3rd Qu.:59.00 3rd Qu.:58.00
## Max. :76.00 Max. :67.00 Max. :75.00 Max. :74.00
## socst
## Min. :26.00
## 1st Qu.:46.00
## Median :52.00
## Mean :52.41
## 3rd Qu.:61.00
## Max. :71.00
```r
library(e1071)
## Warning: 套件 'e1071' 是用 R 版本 4.1.2 來建造的
library(moments)
## Warning: 套件 'moments' 是用 R 版本 4.1.1 來建造的
##
## 載入套件:'moments'
## 下列物件被遮斷自 'package:e1071':
##
## kurtosis, moment, skewness
aggregate(cbind(read, write, math, science, socst) ~ gender, data = hsb, mean)
## gender read write math science socst
## 1 female 51.73394 54.99083 52.39450 50.69725 52.91743
## 2 male 52.82418 50.12088 52.94505 53.23077 51.79121
aggregate(cbind(read, write, math, science, socst) ~ gender, data = hsb, sd)
## gender read write math science socst
## 1 female 10.05783 8.133715 9.151015 9.038503 10.23441
## 2 male 10.50671 10.305161 9.664784 10.732171 11.33384
aggregate(cbind(read, write, math, science, socst) ~ gender, data = hsb, skewness)
## gender read write math science socst
## 1 female 0.32341745 -0.5899993 0.2346739 -0.130718 -0.3532812
## 2 male 0.04674873 -0.1798980 0.3256960 -0.345221 -0.3713532
aggregate(cbind(read, write, math, science, socst) ~ gender, data = hsb, kurtosis)
## gender read write math science socst
## 1 female 2.500028 2.544105 2.284784 2.510875 2.519207
## 2 male 2.262737 1.872877 2.356806 2.371868 2.335229
aggregate(cbind(read, write, math, science, socst) ~ race, data = hsb, mean)
## race read write math science socst
## 1 african-amer 46.80000 48.20000 46.75000 42.80000 49.45000
## 2 asian 51.90909 58.00000 57.27273 51.45455 51.00000
## 3 hispanic 46.66667 46.45833 47.41667 45.37500 47.79167
## 4 white 53.92414 54.05517 53.97241 54.20000 53.68276
aggregate(cbind(read, write, math, science, socst) ~ race, data = hsb, sd)
## race read write math science socst
## 1 african-amer 7.120024 9.322299 6.487843 9.445690 10.850540
## 2 asian 7.660999 7.899367 10.120187 9.490665 9.746794
## 3 hispanic 10.239169 8.272422 6.983936 8.218815 9.250049
## 4 white 10.276783 9.172558 9.383011 9.094870 10.813253
aggregate(cbind(read, write, math, science, socst) ~ race, data = hsb, skewness)
## race read write math science socst
## 1 african-amer 0.56341685 0.2445555 1.5769616 0.1618054 -0.3779222
## 2 asian -0.14903573 -0.8732263 -0.2921502 -0.3239360 0.3398069
## 3 hispanic 0.64286691 0.3420989 0.1538455 0.2121507 0.1741367
## 4 white 0.05686143 -0.7452076 0.1120681 -0.2394562 -0.5568882
aggregate(cbind(read, write, math, science, socst) ~ race, data = hsb, kurtosis)
## race read write math science socst
## 1 african-amer 4.394131 2.069851 5.739716 1.833490 2.553280
## 2 asian 2.457166 2.518799 1.843283 2.240411 2.849030
## 3 hispanic 3.381917 2.777992 2.494265 3.361737 2.218556
## 4 white 2.203770 2.710911 2.351510 2.506333 2.641000
my_summary <- function(x) {
require(moments)
funs <- c(mean, sd, skewness, kurtosis)
sapply(funs, function(f) f(x, na.rm = TRUE))
}
sapply(hsb[, c(7:11)], my_summary)
## read write math science socst
## [1,] 52.2300000 52.7750000 52.6450000 51.8500000 52.4050000
## [2,] 10.2529368 9.4785860 9.3684478 9.9008908 10.7357935
## [3,] 0.1948373 -0.4784158 0.2844115 -0.1872277 -0.3786624
## [4,] 2.3630520 2.2385271 2.3373193 2.4283076 2.4585395