Skip to content

Instantly share code, notes, and snippets.

@ybenjo
Created May 28, 2012 14:18
Show Gist options
  • Save ybenjo/2819411 to your computer and use it in GitHub Desktop.
Save ybenjo/2819411 to your computer and use it in GitHub Desktop.
eval使ってformula書きたい
> # 決定木を使いたい
> library(mvpart)
>
> # 本来なら直接
> # rpart(iris$Species~., iris)
> # とやりたいけど iris の予測すべきラベル名がわからず
> # データフレームの最後にあることだけはわかる,みたいな状況
>
> # ここで最終行の列名取得
> label <- colnames(iris)[ncol(iris)]
> label
[1] "Species"
> # ここでコマンド組立
> command <- paste("rpart(iris$", label, "~., iris)", sep="")
> command
[1] "rpart(iris$Species~., iris)"
> # あとは eval で評価
> eval(parse(text=command))
n= 150
node), split, n, loss, yval, (yprob)
* denotes terminal node
1) root 150 100 setosa (0.33333333 0.33333333 0.33333333)
2) Petal.Length< 2.45 50 0 setosa (1.00000000 0.00000000 0.00000000) *
3) Petal.Length>=2.45 100 50 versicolor (0.00000000 0.50000000 0.50000000)
6) Petal.Width< 1.75 54 5 versicolor (0.00000000 0.90740741 0.09259259)
12) Petal.Length< 4.95 48 1 versicolor (0.00000000 0.97916667 0.02083333) *
13) Petal.Length>=4.95 6 2 virginica (0.00000000 0.33333333 0.66666667) *
7) Petal.Width>=1.75 46 1 virginica (0.00000000 0.02173913 0.97826087) *
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment