Мне нужно было изменить масштаб моих значений y. Этот код работает:
library(datasets)
data2 <- iris[iris$Species != "setosa", ]
data2["Species"] <- as.numeric(unlist(data2["Species"]))
x<- data2$Sepal.Length
y <- data2$Species
y <- rescale(data2$Species, to = c(0, 1), from = range(x, na.rm = TRUE, finite = TRUE))
fit <- lm(y ~ poly(x, 3)) ## polynomial of degree 3
plot(x, y) ## scatter plot (colour: black)
x0 <- seq(min(x), max(x), length = 20) ## prediction grid
y0 <- predict.lm(fit, newdata = list(x = x0)) ## predicted values
lines(x0, y0, col = 2)