使用 cv.tree 时出错 [英] Error when using cv.tree
问题描述
我尝试使用 tree
包中的函数 cv.tree
.我有一个二元分类响应(称为 Label
)和 30 个预测变量.我使用所有预测变量拟合一个树对象.
Hi I tried using the function cv.tree
from the package tree
. I have a binary categorical response (called Label
) and 30 predictors. I fit a tree object using all predictors.
我收到以下我不明白的错误消息:
I got the following error message that I don't understand:
as.data.frame.default(data, optional = TRUE) 中的错误:无法将类函数"强制转换为 data.frame
Error in as.data.frame.default(data, optional = TRUE) : cannot coerce class ""function"" to a data.frame
数据是取自 this 站点的培训"文件.
The data is the file 'training' taken from this site.
这就是我所做的:
x <- read.csv("training.csv")
attach(x)
library(tree)
Tree <- tree(Label~., x, subset=sample(1:nrow(x), nrow(x)/2))
CV <- cv.tree(Tree,FUN=prune.misclass)
推荐答案
cv.tree 调用 model.frame 时发生错误.树对象的调用"元素必须包含对数据框的引用,该数据框的名称也不是已加载函数的名称.
The error occurs once cv.tree calls model.frame. The 'call' element of the tree object must contain a reference to a data frame whose name is also not the name of a loaded function.
因此,当 cv.tree 稍后使用树对象的call"元素使用数据帧时,不仅会在对 tree 的调用中生成子集生成错误使用df"这样的名称也会出错,因为 model.frame 会将其作为现有函数的名称(即 stats 包中的F 分布密度").
Thus, not only will subsetting in the call to tree generate the error when cv.tree later uses the 'call' element of the tree object, using a dataframe with a name like "df" would give an error as well because model.frame will take this to be name of an existing function (i.e. the 'density of F distribution' from the stats package).
这篇关于使用 cv.tree 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!