Reputation: 115
I have epidemiologic data. I am trying to plot the regression:
y = 76.450438 + 0.047259*x_1 + 0.882275*x_2
I found this regression from analysis using my data. I have tried to use plot3d, planes3d, plotly, and a number of other commands to plot this plane (this is a plane, correct?). And yes, I made sure there are no errors regarding libraries or other syntax. I am at the point where the input is accepted by RStudio, but the 3D plot comes up empty, no box, no points, just the letters x y and z in thin air.
Also, is it possible to overlay one regression plane on another in the same 3D plot? I want to compare two regression models within the same plot if possible. Is it also possible to overlay the regression plane on a scatterplot of the data?
Any advice is much appreciated. Thank you.
EpiSimulationRelevant$age <- x
EpiSimulationRelevant$totchol <- y
EpiSimulationRelevant$sysbp <- z
fit <- lm(z ~ x + y)
coefs <- coef(fit)
a <- coefs["x"]
b <- coefs["y"]
c <- -1
d <- coefs["(Intercept)"]
Upvotes: 1
Views: 396
Reputation: 526
Unlike standard base graphics, the plotting functions in plot3D or rgl do not take functions as input, so you have to create the points yourself and then plot the concrete results.
I.e. this works:
x1 <- runif( 1000 )
x2 <- runif( 1000 )
f <- function( x1, x2 ) { 76.450438 + 0.047259 * x1 + 0.882275 * x2 }
m <- cbind( x1, x2, f( x1, x2 ) )
rgl::plot3d( m )
EDIT: Since you created your regression using the lm function, the result of that function is a model object that has its own generic methods for plotting. You should play around with those and see if some of those fit your needs. Did you try simply doing plot( fit )
?
Upvotes: 2