R 从插入符号序列对象中提取要素类/类型

R 从插入符号序列对象中提取要素类/类型,r,machine-learning,r-caret,feature-extraction,R,Machine Learning,R Caret,Feature Extraction,使用内置的iris数据集,我可以对模型进行如下训练: model <- train(Species~., data=iris, method='xgbTree') 但是,当您尝试输入另一种类型的变量来进行预测时,插入符号似乎知道预期的类型 test<- data.frame(Sepal.Length=1, Sepal.Width=2, Petal.Length=3, P

使用内置的iris数据集,我可以对模型进行如下训练:

model <- train(Species~., data=iris, method='xgbTree')
但是,当您尝试输入另一种类型的变量来进行预测时,插入符号似乎知道预期的类型

test<- data.frame(Sepal.Length=1, 
                  Sepal.Width=2, 
                  Petal.Length=3, 
                  Petal.Width="x") # character instead of numeric

predict(model, newdata=test)
## Error: variable 'Petal.Width' was fitted with type "numeric" but type "factor" was supplied

此信息存储为模型术语的属性

attr(terms(model), "dataClasses")
#      Species Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#     "factor"    "numeric"    "numeric"    "numeric"    "numeric" 

非常感谢,我知道它一定在那里的某个地方。
model_types(model$coefnames)
##  "numeric" "numeric" "numeric" "numeric"
attr(terms(model), "dataClasses")
#      Species Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#     "factor"    "numeric"    "numeric"    "numeric"    "numeric"