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 來建造的
library(e1071)
## Warning: 套件 'e1071' 是用 R 版本 4.1.2 來建造的
library(moments)
## Warning: 套件 'moments' 是用 R 版本 4.1.1 來建造的
## 
## 載入套件:'moments'
## 下列物件被遮斷自 'package:e1071':
## 
##     kurtosis, moment, skewness
data("hsb")
str(hsb)
## 'data.frame':    200 obs. of  11 variables:
##  $ id     : int  70 121 86 141 172 113 50 11 84 48 ...
##  $ gender : Factor w/ 2 levels "female","male": 2 1 2 2 2 2 2 2 2 2 ...
##  $ race   : Factor w/ 4 levels "african-amer",..: 4 4 4 4 4 4 1 3 4 1 ...
##  $ ses    : Factor w/ 3 levels "high","low","middle": 2 3 1 1 3 3 3 3 3 3 ...
##  $ schtyp : Factor w/ 2 levels "private","public": 2 2 2 2 2 2 2 2 2 2 ...
##  $ prog   : Factor w/ 3 levels "academic","general",..: 2 3 2 3 1 1 2 1 2 1 ...
##  $ read   : int  57 68 44 63 47 44 50 34 63 57 ...
##  $ write  : int  52 59 33 44 52 52 59 46 57 55 ...
##  $ math   : int  41 53 54 47 57 51 42 45 54 52 ...
##  $ science: int  47 63 58 53 53 63 53 39 58 50 ...
##  $ socst  : int  57 61 31 56 61 61 61 36 51 51 ...
head(hsb)
##    id gender  race    ses schtyp     prog read write math science socst
## 1  70   male white    low public  general   57    52   41      47    57
## 2 121 female white middle public vocation   68    59   53      63    61
## 3  86   male white   high public  general   44    33   54      58    31
## 4 141   male white   high public vocation   63    44   47      53    56
## 5 172   male white middle public academic   47    52   57      53    61
## 6 113   male white middle public academic   44    52   51      63    61
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
#平均數
aggregate(cbind(read, write, math, science, socst) ~ gender+race, data = hsb, mean)
##   gender         race     read    write     math  science    socst
## 1 female african-amer 46.76923 48.84615 47.53846 40.69231 49.69231
## 2   male african-amer 46.85714 47.00000 45.28571 46.71429 49.00000
## 3 female        asian 51.75000 58.87500 56.75000 50.87500 52.25000
## 4   male        asian 52.33333 55.66667 58.66667 53.00000 47.66667
## 5 female     hispanic 45.90909 48.90909 45.27273 45.18182 50.27273
## 6   male     hispanic 47.30769 44.38462 49.23077 45.53846 45.69231
## 7 female        white 53.40260 56.49351 53.77922 53.15584 53.90909
## 8   male        white 54.51471 51.29412 54.19118 55.38235 53.42647
#標準差
aggregate(cbind(read, write, math, science, socst) ~ gender+race, data = hsb, sd)
##   gender         race       read     write      math   science     socst
## 1 female african-amer  7.2933057  9.546781  7.378069  9.525270 11.535341
## 2   male african-amer  7.3581830  9.504385  4.535574  8.577379 10.311806
## 3 female        asian  9.1456469  7.529703 11.106626 10.920982 10.606602
## 4   male        asian  0.5773503 10.115994  8.736895  5.291503  7.637626
## 5 female     hispanic 12.2429943  6.744695  6.842381  5.492143  9.100450
## 6   male     hispanic  8.6639542  9.115358  6.833177 10.211231  9.195874
## 7 female        white  9.7969648  7.261331  8.807329  7.691528 10.136154
## 8   male        white 10.8373217 10.317438 10.056938 10.392347 11.603288
#偏態
aggregate(cbind(read, write, math, science, socst) ~ gender+race, data = hsb, skewness)
##   gender         race       read      write        math     science       socst
## 1 female african-amer  1.0908453  0.2163986  1.48488184  0.73940428 -0.30003839
## 2   male african-amer -0.5014614  0.2805474  0.21617314 -0.98118254 -0.64522406
## 3 female        asian -0.0720213 -0.8936840 -0.18751759 -0.15405549  0.13198759
## 4   male        asian  0.7071068 -0.6993411 -0.45563859 -0.59517006  0.38180177
## 5 female     hispanic  0.8336115  0.4399599 -0.07735766  0.56561271  0.43962390
## 6   male     hispanic  0.3505098  0.6740362  0.38302113  0.10751219  0.01372901
## 7 female        white  0.2652688 -0.8495136  0.05270082  0.01337789 -0.49165481
## 8   male        white -0.1477733 -0.3818725  0.14096486 -0.50797997 -0.58469114
#峰度
aggregate(cbind(read, write, math, science, socst) ~ gender+race, data = hsb, kurtosis)
##   gender         race     read    write     math  science    socst
## 1 female african-amer 5.293553 2.441087 4.698841 2.597492 2.554017
## 2   male african-amer 2.571704 1.213593 1.944268 2.552533 2.315460
## 3 female        asian 1.788177 2.911449 1.718401 1.825103 2.708239
## 4   male        asian 1.500000 1.500000 1.500000 1.500000 1.500000
## 5 female     hispanic 3.454724 1.920618 2.362541 2.095000 1.880657
## 6   male     hispanic 2.303890 3.191498 1.951063 2.659911 1.923078
## 7 female        white 2.254607 3.211956 2.336165 2.573160 2.609000
## 8   male        white 2.193766 2.045456 2.284359 2.425587 2.560890
 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
#數字代表: 1=平均數;2=標準差;3=偏態;4=峰度
with(hsb, aggregate(hsb[, 7:11], by = list(gender, race), FUN = my_summary))
##   Group.1      Group.2     read.1     read.2     read.3     read.4    write.1
## 1  female african-amer 46.7692308  7.2933057  1.0908453  5.2935531 48.8461538
## 2    male african-amer 46.8571429  7.3581830 -0.5014614  2.5717042 47.0000000
## 3  female        asian 51.7500000  9.1456469 -0.0720213  1.7881765 58.8750000
## 4    male        asian 52.3333333  0.5773503  0.7071068  1.5000000 55.6666667
## 5  female     hispanic 45.9090909 12.2429943  0.8336115  3.4547239 48.9090909
## 6    male     hispanic 47.3076923  8.6639542  0.3505098  2.3038901 44.3846154
## 7  female        white 53.4025974  9.7969648  0.2652688  2.2546065 56.4935065
## 8    male        white 54.5147059 10.8373217 -0.1477733  2.1937664 51.2941176
##      write.2    write.3    write.4      math.1      math.2      math.3
## 1  9.5467809  0.2163986  2.4410868 47.53846154  7.37806868  1.48488184
## 2  9.5043850  0.2805474  1.2135932 45.28571429  4.53557368  0.21617314
## 3  7.5297031 -0.8936840  2.9114493 56.75000000 11.10662608 -0.18751759
## 4 10.1159939 -0.6993411  1.5000000 58.66666667  8.73689495 -0.45563859
## 5  6.7446949  0.4399599  1.9206184 45.27272727  6.84238130 -0.07735766
## 6  9.1153576  0.6740362  3.1914975 49.23076923  6.83317698  0.38302113
## 7  7.2613312 -0.8495136  3.2119560 53.77922078  8.80732855  0.05270082
## 8 10.3174375 -0.3818725  2.0454564 54.19117647 10.05693843  0.14096486
##        math.4   science.1   science.2   science.3   science.4     socst.1
## 1  4.69884144 40.69230769  9.52527003  0.73940428  2.59749181 49.69230769
## 2  1.94426762 46.71428571  8.57737889 -0.98118254  2.55253276 49.00000000
## 3  1.71840106 50.87500000 10.92098243 -0.15405549  1.82510261 52.25000000
## 4  1.50000000 53.00000000  5.29150262 -0.59517006  1.50000000 47.66666667
## 5  2.36254116 45.18181818  5.49214315  0.56561271  2.09499993 50.27272727
## 6  1.95106288 45.53846154 10.21123062  0.10751219  2.65991060 45.69230769
## 7  2.33616461 53.15584416  7.69152804  0.01337789  2.57316021 53.90909091
## 8  2.28435866 55.38235294 10.39234709 -0.50797997  2.42558706 53.42647059
##       socst.2     socst.3     socst.4
## 1 11.53534146 -0.30003839  2.55401720
## 2 10.31180553 -0.64522406  2.31545975
## 3 10.60660172  0.13198759  2.70823885
## 4  7.63762616  0.38180177  1.50000000
## 5  9.10044954  0.43962390  1.88065695
## 6  9.19587421  0.01372901  1.92307798
## 7 10.13615444 -0.49165481  2.60899977
## 8 11.60328849 -0.58469114  2.56089041