我正在试验 R 包 gbm2sas 和 gbm。
我正在尝试创建一个 gbm 模型对象(使用 gbm() 函数)并生成将实现该模型的 SAS 代码(使用 gbm2sas() 函数)。我无法让它工作。我收到以下错误。
这是我的 R 代码:
library(gbm)
library(gbm2sas)
data(iris)
iris$setosaFlag = (iris$Species == "setosa")*1
iris.gbm = gbm(setosaFlag ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
data=iris,
dist="bernoulli",
n.tree = 3,
interaction.depth=3,
shrinkage = 0.01,
keep.data=TRUE,
verbose=TRUE,
n.cores=1)
print(iris.gbm)
pretty.gbm.tree(iris.gbm, i.tree=1)
pretty.gbm.tree(iris.gbm, i.tree=2)
pretty.gbm.tree(iris.gbm, i.tree=3)
gbm2sas(
iris.gbm, # gbm object from above
sasfile="studyGBM.R", # name to use for SAS code file
ntrees=3, # number of trees
mysasdata="sasdataset",
treeval="treevalue",
prefix="dobranch_"
)
我得到以下输出和错误:
> library(gbm)
> library(gbm2sas)
> data(iris)
> iris$setosaFlag = (iris$Species == "setosa")*1
> iris.gbm = gbm(setosaFlag ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
+ data=iris,
+ dist="bernoulli",
+ n.tree = 3,
+ interaction.depth=3,
+ shrinkage = 0.01,
+ keep.data=TRUE,
+ verbose=TRUE,
+ n.cores=1)
Iter TrainDeviance ValidDeviance StepSize Improve
1 1.2531 nan 0.0100 0.0096
2 1.2337 nan 0.0100 0.0093
3 1.2148 nan 0.0100 0.0082
> print(iris.gbm)
gbm(formula = setosaFlag ~ Sepal.Length + Sepal.Width + Petal.Length +
Petal.Width, distribution = "bernoulli", data = iris, n.trees = 3,
interaction.depth = 3, shrinkage = 0.01, keep.data = TRUE,
verbose = TRUE, n.cores = 1)
A gradient boosted model with bernoulli loss function.
3 iterations were performed.
There were 4 predictors of which 3 had non-zero influence.
> pretty.gbm.tree(iris.gbm, i.tree=1)
SplitVar SplitCodePred LeftNode RightNode MissingNode ErrorReduction Weight Prediction
0 2 2.4500 1 5 9 1.72800e+01 75 0.0012
1 0 5.0500 2 3 4 3.28692e-31 27 0.0300
2 -1 0.0300 -1 -1 -1 0.00000e+00 15 0.0300
3 -1 0.0300 -1 -1 -1 0.00000e+00 12 0.0300
4 -1 0.0300 -1 -1 -1 0.00000e+00 27 0.0300
5 0 6.8500 6 7 8 5.48890e-30 48 -0.0150
6 -1 -0.0150 -1 -1 -1 0.00000e+00 38 -0.0150
7 -1 -0.0150 -1 -1 -1 0.00000e+00 10 -0.0150
8 -1 -0.0150 -1 -1 -1 0.00000e+00 48 -0.0150
9 -1 0.0012 -1 -1 -1 0.00000e+00 75 0.0012
> pretty.gbm.tree(iris.gbm, i.tree=2)
SplitVar SplitCodePred LeftNode RightNode MissingNode ErrorReduction Weight Prediction
0 2 2.35000000 1 5 9 1.693529e+01 75 0.00103485
1 3 0.25000000 2 3 4 3.104314e-31 27 0.02940891
2 -1 0.02940891 -1 -1 -1 0.000000e+00 17 0.02940891
3 -1 0.02940891 -1 -1 -1 0.000000e+00 10 0.02940891
4 -1 0.02940891 -1 -1 -1 0.000000e+00 27 0.02940891
5 3 2.05000000 6 7 8 1.672221e-30 48 -0.01492556
6 -1 -0.01492556 -1 -1 -1 0.000000e+00 37 -0.01492556
7 -1 -0.01492556 -1 -1 -1 0.000000e+00 11 -0.01492556
8 -1 -0.01492556 -1 -1 -1 0.000000e+00 48 -0.01492556
9 -1 0.00103485 -1 -1 -1 0.000000e+00 75 0.00103485
> pretty.gbm.tree(iris.gbm, i.tree=3)
SplitVar SplitCodePred LeftNode RightNode MissingNode ErrorReduction Weight Prediction
0 2 2.700000000 1 5 9 1.762206e+01 75 0.003792325
1 0 5.050000000 2 3 4 1.479114e-30 32 0.028846427
2 -1 0.028846427 -1 -1 -1 0.000000e+00 20 0.028846427
3 -1 0.028846427 -1 -1 -1 0.000000e+00 12 0.028846427
4 -1 0.028846427 -1 -1 -1 0.000000e+00 32 0.028846427
5 0 6.750000000 6 7 8 8.513506e-31 43 -0.014852589
6 -1 -0.014852589 -1 -1 -1 0.000000e+00 33 -0.014852589
7 -1 -0.014852589 -1 -1 -1 0.000000e+00 10 -0.014852589
8 -1 -0.014852589 -1 -1 -1 0.000000e+00 43 -0.014852589
9 -1 0.003792325 -1 -1 -1 0.000000e+00 75 0.003792325
>
> gbm2sas(
+ iris.gbm, # gbm object from above
+ sasfile="studyGBM.R", # name to use for SAS code file
+ ntrees=3, # number of trees
+ mysasdata="sasdataset",
+ treeval="treevalue",
+ prefix="dobranch_"
+ )
Error in data[, gbmobject$var.names] :
object of type 'closure' is not subsettable
>
>
谁能指出我做错了什么?
谢谢。