Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am new data science and I am learning data science using R. I have used the CART train function to train my model but I am getting the error.

The code is as follows:

control <- trainControl(method="cv", number=10)

lassogrid <- expand.grid(

    alpha=1, lambda=seq(0.001, 0.1, by=0.0005)

)

lasso_model <- train(x=train0[,-c(which(names(train0)=="SalePrice"))], 

                y=train0$SalePrice, method = "glmnet", trControl="control", 

               tuneGrid="lassogrid")

I am getting error as below:

Error: $ operator is invalid for atomic vector

I researched it and found it is not because of the train(). I have no idea about the atomic vector. Can anyone tell me what is the error and how to solve it?

1 Answer

0 votes
by (36.8k points)

Since you have used trControl="control" and tuneGrid="lassogrid" your getting error. It have to be trControl=control and tuneGrid=lassogrid. Since you have no data you need not provide x and y-axis. I am using the BostonHousing data from mlbench package. I have provided the code below you can use it:

library(mlbench)

library(caret)

data <- BostonHousing

# create a list of 70% of the rows in the original dataset we can use for training

set.seed(123)

training <- sample(nrow(data), 0.7 * nrow(data))

train0 <- data[training,]

test0 <- data[-training,]

control <- trainControl(method="cv", number=10)

lassogrid <- expand.grid(

  alpha=1, lambda=seq(0.001, 0.1, by=0.0005)

)

lasso_model <- train(medv~., data= train0, method="glmnet", trControl=control, 

                     tuneGrid=lassogrid)

If you are a beginner and want to know more about Data Science the do check out the Data Science course

 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...