Exercise 1

# 讀取package MASS
library(MASS)
ls("package:MASS")
##   [1] "abbey"             "accdeaths"         "addterm"          
##   [4] "Aids2"             "Animals"           "anorexia"         
##   [7] "area"              "as.fractions"      "bacteria"         
##  [10] "bandwidth.nrd"     "bcv"               "beav1"            
##  [13] "beav2"             "biopsy"            "birthwt"          
##  [16] "Boston"            "boxcox"            "cabbages"         
##  [19] "caith"             "Cars93"            "cats"             
##  [22] "cement"            "chem"              "con2tr"           
##  [25] "contr.sdif"        "coop"              "corresp"          
##  [28] "cov.mcd"           "cov.mve"           "cov.rob"          
##  [31] "cov.trob"          "cpus"              "crabs"            
##  [34] "Cushings"          "DDT"               "deaths"           
##  [37] "denumerate"        "dose.p"            "drivers"          
##  [40] "dropterm"          "eagles"            "enlist"           
##  [43] "epil"              "eqscplot"          "farms"            
##  [46] "fbeta"             "fgl"               "fitdistr"         
##  [49] "forbes"            "fractions"         "frequency.polygon"
##  [52] "GAGurine"          "galaxies"          "gamma.dispersion" 
##  [55] "gamma.shape"       "gehan"             "genotype"         
##  [58] "geyser"            "gilgais"           "ginv"             
##  [61] "glm.convert"       "glm.nb"            "glmmPQL"          
##  [64] "hills"             "hist.FD"           "hist.scott"       
##  [67] "housing"           "huber"             "hubers"           
##  [70] "immer"             "Insurance"         "is.fractions"     
##  [73] "isoMDS"            "kde2d"             "lda"              
##  [76] "ldahist"           "leuk"              "lm.gls"           
##  [79] "lm.ridge"          "lmsreg"            "lmwork"           
##  [82] "loglm"             "loglm1"            "logtrans"         
##  [85] "lqs"               "lqs.formula"       "ltsreg"           
##  [88] "mammals"           "mca"               "mcycle"           
##  [91] "Melanoma"          "menarche"          "michelson"        
##  [94] "minn38"            "motors"            "muscle"           
##  [97] "mvrnorm"           "nclass.freq"       "neg.bin"          
## [100] "negative.binomial" "negexp.SSival"     "newcomb"          
## [103] "nlschools"         "npk"               "npr1"             
## [106] "Null"              "oats"              "OME"              
## [109] "painters"          "parcoord"          "petrol"           
## [112] "phones"            "Pima.te"           "Pima.tr"          
## [115] "Pima.tr2"          "polr"              "psi.bisquare"     
## [118] "psi.hampel"        "psi.huber"         "qda"              
## [121] "quine"             "Rabbit"            "rational"         
## [124] "renumerate"        "rlm"               "rms.curv"         
## [127] "rnegbin"           "road"              "rotifer"          
## [130] "Rubber"            "sammon"            "select"           
## [133] "Shepard"           "ships"             "shoes"            
## [136] "shrimp"            "shuttle"           "Sitka"            
## [139] "Sitka89"           "Skye"              "snails"           
## [142] "SP500"             "stdres"            "steam"            
## [145] "stepAIC"           "stormer"           "studres"          
## [148] "survey"            "synth.te"          "synth.tr"         
## [151] "theta.md"          "theta.ml"          "theta.mm"         
## [154] "topo"              "Traffic"           "truehist"         
## [157] "ucv"               "UScereal"          "UScrime"          
## [160] "VA"                "waders"            "whiteside"        
## [163] "width.SJ"          "write.matrix"      "wtloss"
# 發現檔案有165個項目,接著列出94~103項
ls("package:MASS")[94:103]
##  [1] "minn38"            "motors"            "muscle"           
##  [4] "mvrnorm"           "nclass.freq"       "neg.bin"          
##  [7] "negative.binomial" "negexp.SSival"     "newcomb"          
## [10] "nlschools"

Exercise 2

# 讀入檔案 women
dta<-women
# 計算中心化高度
dta$height_centered <- with(dta,(height-mean(height)))
# 計算係數
coef(lm(weight ~ height_centered, data = dta))
##     (Intercept) height_centered 
##        136.7333          3.4500
#由於中心化只會產生中心水平移動的現象,並不會影響變項之關聯與產生斜率之改變

Exercise 3

# 此處將women當中的height與weight視為兩個不同之date frame進行合併
c(women)
## $height
##  [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## 
## $weight
##  [1] 115 117 120 123 126 129 132 135 139 142 146 150 154 159 164
# 此處將women當中的height與weight以矩陣形式進行合併
c(as.matrix(women))
##  [1]  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72 115 117
## [18] 120 123 126 129 132 135 139 142 146 150 154 159 164

Exercise 4

# 先對於檔案birthwt{MASS}進行初步檢視
head(birthwt)
##    low age lwt race smoke ptl ht ui ftv  bwt
## 85   0  19 182    2     0   0  0  1   0 2523
## 86   0  33 155    3     0   0  0  0   3 2551
## 87   0  20 105    1     1   0  0  0   1 2557
## 88   0  21 108    1     1   0  0  1   2 2594
## 89   0  18 107    1     1   0  0  1   0 2600
## 91   0  21 124    3     0   0  0  0   0 2622
# 再透過coding檢視對於race進行分類後之差異
c("White", "Black", "Other")[birthwt$race]
##   [1] "Black" "Other" "White" "White" "White" "Other" "White" "Other"
##   [9] "White" "White" "Other" "Other" "Other" "Other" "White" "White"
##  [17] "Black" "White" "Other" "White" "Other" "White" "White" "Other"
##  [25] "Other" "White" "White" "White" "Black" "Black" "Black" "White"
##  [33] "Black" "White" "Black" "White" "White" "White" "White" "White"
##  [41] "Black" "White" "Black" "White" "White" "White" "White" "Other"
##  [49] "White" "Other" "White" "Other" "White" "White" "Other" "Other"
##  [57] "Other" "Other" "Other" "Other" "Other" "Other" "Other" "White"
##  [65] "Other" "Other" "Other" "Other" "White" "Black" "White" "Other"
##  [73] "Other" "Black" "White" "Black" "White" "White" "Black" "White"
##  [81] "White" "White" "Other" "Other" "Other" "Other" "Other" "White"
##  [89] "White" "White" "White" "Other" "White" "White" "White" "White"
##  [97] "White" "White" "White" "White" "White" "White" "Other" "White"
## [105] "Other" "Black" "White" "White" "White" "Black" "White" "Other"
## [113] "White" "White" "White" "Other" "White" "Other" "White" "Other"
## [121] "White" "Other" "White" "White" "White" "White" "White" "White"
## [129] "White" "White" "Other" "White" "Black" "Other" "Other" "Other"
## [137] "Other" "Black" "Other" "White" "White" "White" "Other" "Other"
## [145] "White" "White" "Black" "White" "Other" "Other" "Other" "White"
## [153] "White" "White" "White" "Other" "Black" "White" "Black" "Other"
## [161] "White" "Other" "Other" "Other" "Black" "White" "Other" "Other"
## [169] "White" "White" "Black" "Black" "Black" "Other" "Other" "White"
## [177] "White" "White" "White" "Black" "Other" "Other" "White" "Other"
## [185] "White" "Other" "Other" "Black" "White"
# 可觀察對於單一變項做分類後的呈現與取代,對於資料的檢視與管理可產生簡化之作用