SVM - Error in model.frame.default(object, data, xlev = xlev) : object is not a matrix

41 views Asked by At

can't figure out what I'm doing wrong. I am trying to run an SVM prediction model, as below:

library(terra)
library(e1071)
library(caret)
library(MultiscaleDTM)

APA170 <- rast("bs.tif")
bathy <- rast("bathy.tiff")

SD <- AdjSD(bathy)
rug <- RIE(bathy)
SD<-aggregate(SD,5)
rug <-aggregate(rug,10)
bathy <-aggregate(bathy,5)
APA170 <- aggregate(APA170, 10)

r <- c(APA170, terra::project(bathy, APA170))
r <- c(r, terra::project(SD, r))
r <- c(r, terra::project(rug, r))

r2 <- as.matrix(r)
r2 <- na.omit(r2)

plot(r)
summary(r)

train <- vect("train.shp")
vals <- extract(r, train, fun=mean, ID = FALSE)

train$APA170 <- vals$APA170
train$bathy <- vals$bathy
train$SD <- vals$adjSD
train$rug<-vals$rie

train <- as.data.frame(train)
train = na.omit(train)

preds <- list()
    
for(i in 1:nrow(train)){
train_data <- train[-i, ]
test_data <- train[i, ]
mod <- svm(Rhodolith ~ rug, data = train_data, kernel="radial", type="C-classification",cost=3)
preds[[i]] <- predict(object = mod, newdata = as.matrix(test_data[ ,"rug"]))
}

mod= svm(factor(Rhodolith) ~ rug, data = as.matrix(train), kernel="radial", type="C-classification", cost = 3)

mat <- as.matrix(r)
p <- predict(object = mod, newdata = mat[complete.cases(mat), ])

When I run, I get this error:

Error in model.frame.default(object, data, xlev = xlev) :
object is not a matrix

0

There are 0 answers