Error in model.frame.default : invalid type (S4) for variable

Hello, I am new to RStudio software. I'm trying to use an SVM function with "terra", "e1071", "caret" and "MultiscaleDTM" packages. This is the training command:

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

After I ran the functions, I received this message:

Error in model.frame.default(formula = Rhodolith ~ APA170, data = train_data, :
invalid type (S4) for variable 'APA170'

and

Error in model.frame.default(formula = Rhodolith ~ bathy, data = train_data, :
invalid type (S4) for variable 'bathy'

I thought it might be a class problem, but, no matter the command I use to find a difference between the datasets, I can't seem to find a reason why it shouldn't work.

Investigating "class", I get this:

class(APA170)
[1] "SpatRaster"
attr(,"package")
[1] "terra"
class(bathy)
[1] "SpatRaster"
attr(,"package")
[1] "terra"
class(rug)
[1] "SpatRaster"
attr(,"package")
[1] "terra"
class(SD)
[1] "SpatRaster"
attr(,"package")
[1] "terra"

And, "str", this:

str(APA170)
S4 class 'SpatRaster' [package "terra"]
str(bathy)
S4 class 'SpatRaster' [package "terra"]
str(rug)
S4 class 'SpatRaster' [package "terra"]
str(SD)
S4 class 'SpatRaster' [package "terra"]

Can someone please help me sort this out? Thanks in advance.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.