1

Trying to use a decision tree to predict drug classes, when I set a seed and then set the sample_frac() for my training data & then use setdiff() for my test data im seeing my data within the training data but when i view my training data its showing no data? even tho its showing my decision tree.[[[enter image description here](https://i.stack.imgur.com/wkzA7.png)](https://i.stack.imgur.com/Ltn4v.png)](https://i.stack.imgur.com/nsTcM.png)

data <- data %>% select(BP, Cholesterol, Drug)
view(data)
str(data)

data <- lapply(data, as.factor)
data <- lapply(data, as.numeric)
view(data)


data <- as.data.frame(data)
view(data)

set.seed(111)
data_training <- data %>% sample_frac(0.80)
data_test <- data %>% setdiff(data_training)

view(data_training)
view(data_test)

data_tree <- rpart(Drug ~ BP + Cholesterol, data=data_training, method = 'class')
plot(data_tree, uniform=TRUE, margin=0.5)
text(data_tree, use.n = TRUE)


data_test['Predicted'] <- predict(data_tree, data_test, type = 'class')
view(data_test)
Dan C
  • 11
  • 1
  • 1
    Welcome to SO, Dan C! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. (Note: please no data as (just) images, see https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/.) Thanks! – r2evans Jan 03 '23 at 19:22

0 Answers0