Я пытался создать дерево классификации с помощью команды prune (), но появилась эта ошибка.Может кто-нибудь сказать мне, как это исправить?Большое спасибо.
```{r}
c.tree2 <- prune(c.tree1, cp = 2)#Set cp to the level at which you want the tree to end
#Visualize this tree and compare it to the one you generated earlier
post(c.tree2, file = "tree2.ps", title = "MOOC2") #This creates a pdf image of the tree
Error in plot.rpart(tree, uniform = TRUE, branch = 0.2, compress = TRUE, : fit is not a tree, just a root
Когда я использовал команду rpart, она работала, но не тогда, когда я использовал prune ().
Это остальная часть кода:
#Decision tree
```{r}
#Using the rpart package generate a classification tree predicting certified from the other variables in the M1 data frame. Which variables should you use?
c.tree1 <- rpart(certified ~ forum.posts, method = "class", data = M1)
#Check the results from the classifcation tree using the printcp() command
printcp(c.tree1)
#Plot your tree
post(c.tree1, file = "tree1.ps", title = "MOOC1") #This creates a pdf image of the tree
```
#The heading "xerror" in the printcp table stands for "cross validation error", it is the error rate of assigning students to certified/uncertified of the model averaged over 10-fold cross validation. CP stands for "Cost Complexity" and represents the cost in error for adding a node to the tree. Notice it decreases as we add more nodes to the tree which implies that more nodes make better predictions. However, more nodes also mean that we may be making the model less generalizable, this is known as "overfitting".
#If we are worried about overfitting we can remove nodes form our tree using the prune() command, setting cp to the CP value from the table that corresponds to the number of nodes we want the tree to terminate at. Let's set it to two nodes.
```{r}
c.tree2 <- prune(c.tree1, cp = 2)#Set cp to the level at which you want the tree to end
#Visualize this tree and compare it to the one you generated earlier
post(c.tree2, file = "tree2.ps", title = "MOOC2") #This creates a pdf image of the tree