0
library(tree)
library(ISLR)
data("Carseats")
High<-ifelse(Carseats$Sales<=8,'No','Yes')
Carseats<-data.frame(Carseats,High)
tree.Carseats<-tree(as.factor(High)~.-Sales, data = Carseats)
summary(tree.Carseats)
plot(tree.Carseats)
text(tree.Carseats,pretty=0)
set.seed(1)
train=sample(1:nrow(Carseats),200)

I am writing to ask about the code train=sample(1:nrow(Carseats),200).

The result of this code simply show the data and I cannot use View(train) to see the data set.

IMO,I think for the sample, we are going to choose the row of the dataset data(Carseats), each element should contain some labels such as Sales, Income...

Maybe I am confused with the theoretical ideas behind.

Mathilda Fang
  • 353
  • 1
  • 13
  • 1
    See : https://stackoverflow.com/questions/17200114/how-to-split-data-into-training-testing-sets-using-sample-function – Ronak Shah Jun 25 '21 at 02:58

1 Answers1

1

That train=... line of code is just creating an integer vector of 200 random numbers (between 1 and nrow(Carseats)). You need to actually subset the data. Something like this:

train_index <- sample(nrow(Carseats), 200)
training_data <- Carseats[train_index, ]

Then you'll want your test data to have the remaining observations, e.g.,

test_data <- Carseats[-train_index, ]
heds1
  • 3,203
  • 2
  • 17
  • 32
  • thank you very much @heds1 I make prefect sense – Mathilda Fang Jun 25 '21 at 02:58
  • can I ask what does the comma ```,``` mean in ```[]```? what should I type for help in R. maybe ```?[]```? – Mathilda Fang Jun 25 '21 at 03:31
  • 1
    You can type `?\`[\`` for the help for that, it's data-frame subsetting syntax. I think it would be a good idea to take a beginner R course or work through some of the basics -- it will be hard to do statistical modelling without that foundation :) – heds1 Jun 25 '21 at 03:36
  • I am currently self-studying the book An introduction to statistical learning with application in R :p. If you would recommend some resources for self studying, that would be great @heds1 <3 – Mathilda Fang Jun 25 '21 at 03:42
  • BTW, the book shows ```Carseats.test<-Carseats[-train,]``` and ```High.test=HIgh[-train]``` why one has ```,```, one doesn't – Mathilda Fang Jun 25 '21 at 03:52