ランダムフォレストを少し発展?させるpartyのctreeを使う. partyのvignetteをなぞるだけです.
library(party)
library(dplyr)
library(magrittr)
airq = airquality %>% dplyr::filter(Ozone != "NA")
head(airq)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 28 NA 14.9 66 5 6
## 6 23 299 8.6 65 5 7
Ozoneの推定モデルを, ctreeによって作成する.
airct = ctree(Ozone ~ ., data = airq,
controls = ctree_control(maxsurrogate=3))
plot(airct)
# なにこれ
all.equal(predict(airct, type="node"), where(airct))
## [1] TRUE
次. irisの種類判定モデルをctreeで作成.
iris %>% head
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
irisct = ctree(Species ~ ., data = iris)
plot(irisct)
table(predict(irisct), iris$Species)
##
## setosa versicolor virginica
## setosa 50 0 0
## versicolor 0 49 5
## virginica 0 1 45
よさそうだけど, 他の手法でもこれくらいの性能は出るよね.
次. mammmoexpでordinal regression. 乳がん検査のアンケート結果データらしい.
data("mammoexp", package = "TH.data")
mammoexp %>% head
## ME SYMPT PB HIST BSE DECT
## 1 Never Disagree 7 No Yes Somewhat likely
## 2 Never Agree 11 No Yes Very likely
## 3 Never Disagree 8 Yes Yes Very likely
## 4 Within a Year Disagree 11 No Yes Very likely
## 5 Over a Year Strongly Disagree 7 No Yes Very likely
## 6 Never Disagree 7 No Yes Very likely
順位統計量…
ME: mammogram experience.
Sympt: 兆候が見られなかったら, 今後検査は受けない?
PB: 検査への信頼度みたいなもの
HIST: 母か姉妹に乳がんの人がいるかどうか
Dect: 信頼度? 4択らしい. よく分からん.
mammoct = ctree(ME ~ ., data =mammoexp)
plot(mammoct)
検査を受けたくない人は検査したことない人が多い?
次. 生存時間解析. GBSG2も乳がんのデータ. 乳がん再発?までの時間を解析したい.
library(TH.data)
library(survival)
data("GBSG2", package = "TH.data")
GBSG2 %>% head
## horTh age menostat tsize tgrade pnodes progrec estrec time cens
## 1 no 70 Post 21 II 3 48 66 1814 1
## 2 yes 56 Post 12 II 7 61 77 2018 1
## 3 yes 58 Post 35 II 9 52 271 712 1
## 4 yes 59 Post 17 II 4 60 29 1807 1
## 5 no 73 Post 35 II 1 26 65 772 1
## 6 no 32 Pre 57 III 24 0 13 448 1
GBSG2ct = ctree(Surv(time, cens) ~ ., data = GBSG2)
plot(GBSG2ct)
pnodes: 陽性反応したノード?の数.
horTh: ホルモン療法の有無
progrec: 黄体ホルモン受容体の個数的なもの