Question 1

library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
dta<-ls("package:MASS")
dta
##   [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"

由上面結果可知MASS裡面共有165個項目

顯示第94到103項

dta[c(94:103)]
##  [1] "minn38"            "motors"            "muscle"           
##  [4] "mvrnorm"           "nclass.freq"       "neg.bin"          
##  [7] "negative.binomial" "negexp.SSival"     "newcomb"          
## [10] "nlschools"

Question 2

看women這筆資料前六筆

WOMEN<-women
head(WOMEN)
##   height weight
## 1     58    115
## 2     59    117
## 3     60    120
## 4     61    123
## 5     62    126
## 6     63    129

運算身高平均

mean(WOMEN$height)
## [1] 65

將women的平均身高儲存成變項height_centered

mean_height<-rep(65,15)
WOMEN<-data.frame(WOMEN,mean_height)
height_centered<-with(WOMEN,WOMEN$height-WOMEN$mean_height)
WOMEN<-data.frame(WOMEN,height_centered)

提取迴歸係數

coef(lm(weight~height,data=WOMEN))
## (Intercept)      height 
##   -87.51667     3.45000
coef(lm(weight~height_centered,data=WOMEN))
##     (Intercept) height_centered 
##        136.7333          3.4500

將X作左右平移,並不會影響到斜率(迴歸係數),僅會影響到截距

以圖形佐證

with(WOMEN,plot(weight~height,xlab="height",ylab="weight"))

with(WOMEN,plot(weight~height_centered,xlab="height_centered",ylab="weight"))

Question 3

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
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

指令c(women)是要顯示women所有的物件,不論是數值或是變項名稱。而c(as.matrix(women))僅有擷取數值呈現而已。

Question 4

help("birthwt")

從help(“birthwt”)內容可以看出Race是使用1,2,3來表白人、黑人、其它人種

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"

上面code的是將birthwt中race這個變項的代號1,2,3分別帶入white,black,other。