简体   繁体   中英

Error in eval(predvars, data, env) : object 'BMI' not found

I am just starting with Machine Learning with R . My goal is stupid though, please excuse. I am trying to train to comment whether a BMI of a person says Overweight, Underweight or Healthy. Here's my code so far :-

    dataset <- data.frame(
    BMI = c(15.5, 16.7, 17.8, 18.9, 19.0, 19.5, 20.7, 21.9,
            22.5, 23.5, 24.5, 25.0, 26.0, 27.7, 26.6, 29.3),
    Result = c("Underweight", "Underweight", "Underweight", 
                "Healthy", "Healthy", "Healthy", "Healthy", 
                "Healthy", "Healthy", "Healthy", "Healthy", 
                "Overweight", "Overweight", "Overweight", 
                "Overweight", "Overweight")
    )
    head(dataset)
    x <- dataset[,-2]
    y <- dataset[2]
    model_svm <- svm(Result ~ ., data = dataset)
    summary(model_svm)
    pred <- predict(model_svm, x)

But I am getting this error on the last line in predict :-

Error in eval(predvars, data, env) : object 'BMI' not found

When subsetting your dataset to create x (and y ), you are implicitly dropping the dimensions and only retaining the column vector. In other words, x is a vector , not a data frame. This is an annoying property of base R dataframe subset operator.

You can avoid it like this:

x <- dataset[, -2, drop = FALSE]

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