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.3 來建造的
library(lattice)
## 
## 載入套件:'lattice'
## 下列物件被遮斷自 'package:faraway':
## 
##     melanoma

View data

連續變項

data(hsb)
View(hsb) #View hsb
dim(hsb) ##Columns and rows
## [1] 200  11
str(hsb) #Structure
## '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 ...
#描述統計
summary(hsb) #Summary
##        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

The data was collected as a subset of the “High School and Beyond” study conducted by the National Education Longitudinal Studies (NELS) program of the National Center for Education Statistics (NCES).

Detail of variables: id:ID of student gender:a factor with levels female male race:a factor with levels african-amer asian hispanic white ses:socioeconomic class - a factor with levels high low middle schtyp:school type - a factor with levels private public prog:choice of high school program - a factor with levels academic general vocation read:reading score write:writing score math:math score science:science score socst:social science score

As the results of function show out, we can find that hsb contains 200 rows and 11 columns.

plot(density(hsb$id));plot(density(hsb$read));plot(density(hsb$write));plot(density(hsb$math));plot(density(hsb$science)) #density plot

#社會科學分數的分佈狀況
par(mfrow=c(3,3))
hist(hsb$socst)

類別變項

描述統計

#Frequencies
table(hsb$gender);table(hsb$race);table(hsb$ses);table(hsb$schtyp);table(hsb$prog)
## 
## female   male 
##    109     91
## 
## african-amer        asian     hispanic        white 
##           20           11           24          145
## 
##   high    low middle 
##     58     47     95
## 
## private  public 
##      32     168
## 
## academic  general vocation 
##      105       45       50

The relationship between gender and choice of high school program(prog)?

根據下面結果,無論性別,多數學生都選擇了academic program,其次是vocation program,最後是general program

#Frequencies of gender and choice of high school program
ftable(hsb$gender, hsb$prog)
##         academic general vocation
##                                  
## female        58      24       27
## male          47      21       23
#Proportions of gender and choice of high school program
round(prop.table(ftable(hsb$gender, hsb$prog)), 3)
##         academic general vocation
##                                  
## female     0.290   0.120    0.135
## male       0.235   0.105    0.115

The relationship between choice of high school program(prog) and social science score ?

高中課程各類別學生在社會科學分數的平均數與標準差

aggregate(socst ~ prog, data = hsb, FUN = mean)
##       prog    socst
## 1 academic 56.69524
## 2  general 50.60000
## 3 vocation 45.02000
aggregate(socst ~ prog, data = hsb, FUN = sd)
##       prog     socst
## 1 academic  9.173670
## 2  general  9.308842
## 3 vocation 10.656971

高中課程各類別學生在社會科學分數間的關係

#高中課程各類別學生在社會科學分數間的關係
densityplot(~ socst, groups = prog, data = hsb, xlab = '社會科學分數', lty = c(1,2), plot.points = F, type = "g", , main = ' choice of high school program (academic  = blue, general = purple, vocation = black)')

高中課程各類別學生在社會科學分數的機率分佈

histogram(~ socst | prog, data = hsb, xlab = 'choice of high school program', ylab='機率',
          type = 'density', layout = c(3, 1))

高中課程各類別學生在社會科學分數的盒方圖

boxplot(socst ~ prog, hsb, xlab = "choice of high school program", ylab = "社會科學分數", frame = F, col = c("#FC4E50", "#00AFBC", "#E7B850" ))

如以上圖可以發現高中選擇不同program的學生在社會科學分數上有所不同