下載Package MASS
library(MASS)
dta<-ls("package:MASS")
dta %>% summary()
## Length Class Mode
## 165 character character
List the names of the item indices from 94 to 103
dta[94:103]
## [1] "minn38" "motors" "muscle"
## [4] "mvrnorm" "nclass.freq" "neg.bin"
## [7] "negative.binomial" "negexp.SSival" "newcomb"
## [10] "nlschools"
先把women資料另存到W裡面才能做變動,Height_centered表示把原來的數值檢減去平均數
W<-women
coef(lm(weight ~ height, data = W))
## (Intercept) height
## -87.51667 3.45000
W <- W %>% mutate(height_centered=height-mean(W$height))
knitr::kable(head(W, 10))
| height | weight | height_centered |
|---|---|---|
| 58 | 115 | -7 |
| 59 | 117 | -6 |
| 60 | 120 | -5 |
| 61 | 123 | -4 |
| 62 | 126 | -3 |
| 63 | 129 | -2 |
| 64 | 132 | -1 |
| 65 | 135 | 0 |
| 66 | 139 | 1 |
| 67 | 142 | 2 |
用Height_centered來對weight做回歸,結果如下:
coef(lm(weight ~ height_centered, data = W))
## (Intercept) height_centered
## 136.7333 3.4500
兩者的截距不一樣是因為我們對x變項做了平移(減去平均數)
women本來是data.frame的格式,如果直接用c(women)會讓他變成list
class(women)
## [1] "data.frame"
c(women) %>% class
## [1] "list"
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
如果強制他為matrix,則會讓原本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
原先birthwt$race的output是integer,會看不出來數字代表的意義
birthwt$race %>% class
## [1] "integer"
birthwt$race
## [1] 2 3 1 1 1 3 1 3 1 1 3 3 3 3 1 1 2 1 3 1 3 1 1 3 3 1 1 1 2 2 2 1 2 1 2
## [36] 1 1 1 1 1 2 1 2 1 1 1 1 3 1 3 1 3 1 1 3 3 3 3 3 3 3 3 3 1 3 3 3 3 1 2
## [71] 1 3 3 2 1 2 1 1 2 1 1 1 3 3 3 3 3 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 3 1 3
## [106] 2 1 1 1 2 1 3 1 1 1 3 1 3 1 3 1 3 1 1 1 1 1 1 1 1 3 1 2 3 3 3 3 2 3 1
## [141] 1 1 3 3 1 1 2 1 3 3 3 1 1 1 1 3 2 1 2 3 1 3 3 3 2 1 3 3 1 1 2 2 2 3 3
## [176] 1 1 1 1 2 3 3 1 3 1 3 3 2 1
使用help("birthwt")後知道mother’s race (1 = white, 2 = black, 3 = other), 因此下列程式碼是把數字換成對應的種族字串
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"