Somewhat newbie question here. I'd like to use ggplot2 to plot my multiple regression model, but I ran into an error. I did some research on the forum before deciding to ask the community. The original question was posted in the wrong forum by mistake.
I understand how to plot a simple linear regression:
ggplot(data, aes(x=X, y=Y))+geom_point()+
geom_smooth(method='lm',formula=Y~X)
But when I tried to do it for multiple regression:
Model<-lm(Y~x1*x2*x3*x4*x5, data, na.action=na.omit)
ggplot(data, aes(x=X, y=Model))+geom_point()+
geom_smooth(method='lm',formula=Y~x1*x2*x3*x4*x5)
I get this error:
Don't know how to automatically pick scale for object of type lm. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (209): x, y
Have you tried using abline() function?
try using
abline(Model,lwd=3,col="red")
You could try this for multiple regression if you have two independent variables,
set.seed(1)
data <- data.frame(x1=rnorm(100, 5, 2), x2=rnorm(100, 10, 5)) # some random data
data$y <- 3*data$x1 + 2*data$x2 + rnorm(100, 2, 5)
lmFit <- lm(y ~ x1 + x2, data = data)
x1 <- seq(from = min(data$x1), to = max(data$x1), by = 0.1)
x2 <- seq(from = min(data$x2), to = max(data$x2), by = 0.1)
y <- outer(X = x1, Y = x2, FUN = function(x1, x2) {
predict(lmFit, newdata = data.frame(x1 = x1, x2 = x2))
})
open3d()
bg3d("white")
material3d(col="black")
persp3d(x1, x2, y, col = "green3", xlab = "x1", ylab = "x2", zlab = "y",theta=50, phi=25, expand=0.75, ticktype="detailed")
points3d(data$x1, data$x2, data$y, col = "red", size=5)
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.