简体   繁体   中英

Getting Error in terms..formula: '.' in formula and no data argument in using train() function in lm

New to this community and thanks in advance for any help. I am just getting started in using R for modeling and have run across the above error. This is for a simple formula using mtcars R dataset. Can anyone provide some suggestions to fix it? I ran across similar questions in the archives but couldn't find a solution.

LMfit1 <-train(mtcars$mpg ~ ., data =TrainData, method = "lm")
    
Error in terms.formula(formula, data = data) : 
 '.' in formula and no 'data' argument

Try LMfit1 <- lm(mtcars$mpg ~., data = TrainData)

Also, make sure you have TrainData defined somewhere.

If you can't get that to work, either use

lm(mtcars$mpg ~ ., data = mtcars)

or create a TrainData ( here's an example )

Thanks for responding.
1.This didn't work.
LMfit1<- lm(mtcars$mpg ~., data= TrainData)

  1. This did work. LMfit1<- lm(mtcars$mpg ~., data=mtcars)

  2. I think the problem is in the TrainData set. I know that the TrainData is numeric; I tried converting to data.frame (as.data.frame) but didn't help. Here is the build:

DataSplit <- createDataPartition(y = car_dat$mpg, p=0.7, list= FALSE)

TrainData <- car_dat[DataSplit]

TestData <- car_dat[-DataSplit]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM