I'm trying to graph the object resulting from fitting a decision tree model , but I'm running into several problems:
tree_model<- tree(High~ ShelveLoc + Price + Advertising + Income + Age + CompPrice,
data = data,na.action = "na.exclude", control = tree.control(nobs = 400, minsize = 1))
After executing the above, it returns the following message:
Warning messages:
1: In tree(High ~ ShelveLoc + Price + Advertising + Income + Age + :
NAs introduced by coercion
2: In tree(High ~ ShelveLoc + Price + Advertising + Income + Age + :
NAs introduced by coercion
From what I understand of the message, I think that some missing values may be negatively affecting the fit of the model, so I've tried various options in the argument na.action
, but the same message always appears. On the other hand, I have reviewed the dataframe and there is no missing value, I have also modified the parameters of tree.control
and I still do not get the graph.
Next I try to graph the model:
plot(tree_model)
But the following message appears:
Error in plot.tree(tree_model) : cannot plot singlenode tree
From what I have read, it is usually due to the fact that my data has very little variance and this means that there is only one node in my tree, but since it is a class exercise I know that a tree with several nodes should be generated and it will probably be me is escaping something in the function tree()
. After reviewing the PDF available in the CRAN of the package in question, I still don't know what argument to include or modify in the function.
What do I need to modify in my code to get the graph I want?
If we are facing a classification problem, you have tried to convert your target variable (high) to 'factor'. In case it is not classification or that is not the problem, comment and we will try to find an answer together.
PS: If you allow me some advice for the next occasion, a little context is always appreciated, are you facing a regression or classification problem? What package are you using and what version? this can make it easier for them to help you in the future, depending on how complicated the problems you face become.