1

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)"]
lebelinoz
  • 4,890
  • 10
  • 33
  • 56
imaginov
  • 115
  • 6
  • The trick to plotting functions like this is usually to generate enough (x1, x2, y) data points to plot, in the format the plotting function expects. If you've been attempting that, please show an example of the code to generate the data in your question. – Marius Nov 12 '17 at 23:31
  • I have about 11K+ data points in my set even after omitting the NAs. I've just inserted the code above. – imaginov Nov 12 '17 at 23:41
  • As a recommendation, you should not take a screenshot of the code but instead copy and paste the code into the answer. You can highlight it using Control + K within the Stack Overflow editor. – Michael Harper Nov 12 '17 at 23:43
  • Also, questions like this do really benefit from having an example dataset. You could consider generating a random dataset with R to at least give us something to play with? Consider reading this for some more advice: https://stackoverflow.com/q/5963269/7347699 – Michael Harper Nov 12 '17 at 23:45

1 Answers1

2

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 )?

jtatria
  • 527
  • 3
  • 12
  • May I ask why you've used runif (does that not generate random numbers from the uniform distribution)? Also, is there any other function which allows me to input an equation? With 11K+ points, I don't know how it is possible for me to accurately select a few to represent the distribution. – imaginov Nov 12 '17 at 23:44
  • I used runif just to create some vector. You asked how to plot a function defining a plane, but sadly R's 3D plotting can't take functions as inputs (unlike standard ````plot````), so the usual approach is to instantiate a bunch of points. Runif serves just as well as any ohter input; you should of course use real data if you have it, or create the input vector with a more suitable distribution. – jtatria Nov 12 '17 at 23:47
  • Defining the fit (line 4) gives me this error: Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels In addition: Warning message: In storage.mode(v) <- "double" : NAs introduced by coercion – imaginov Nov 12 '17 at 23:53
  • Also, I receive this error when running the line where m is defined: Error in 0.047259 * x : non-numeric argument to binary operator. I don't understand why this is occurring - all my data points are numeric? – imaginov Nov 12 '17 at 23:54
  • In your code the first three lines are irrelevant, because those are assigning values to members in ````EpiSimulationRelevant````, from x,y,z but then you don't use that object in the rest of the code. Then you fit the model with x,y,z, with no consideration to ````EpiSimulationRelevant````, maybe you intended to do it the other way around (i.e. asiign _to_ x,y,z _from_ ````EpiSimulationRelevant````? If you don't show the full code, there's not much we can do... but the lm is complaining about some problem with x,y,z, which is probably why you get NAs in the result. – jtatria Nov 13 '17 at 00:03
  • I fixed the error of assigning x, y, and z to the variables in the data set. When I plot the fit, however I get a residual plot. Nonetheless, using the code you provided in the main body of text, I am able to view a plane. Is there a way I can overlay another plane over this to compare the regressions? – imaginov Nov 13 '17 at 00:12
  • you can use ````rgl::points3d```` to add more points to the currently active plot (as in base graphics with ````plot```` and ````points````; plot/plot3d create new plots, points/points3d add points to the current plot). You can also combine different sets of points and plot them all at once with ````rbind```` – jtatria Nov 13 '17 at 00:19
  • Thanks. I was able to overlay both planes on each other. I can't seem to find the options menu however, to add colors, axes titles, etc. – imaginov Nov 13 '17 at 01:24
  • Rstudio does not offer a menu to control graphical parameters. Those depend on the arguments to the plotting function you are using. Look at the documentation for whatever function you are using. You can open the help for any function by entering its name with a ?, i.e. ````?rgl::plot3d```` wil bring up the docs for rgl::plot3d. NB: there are _many_ graphical parameters in R, that can be set either globally or per call passing them as arguments. good luck! ps: would you mind accepting the answer if it worked for you? thanks! – jtatria Nov 13 '17 at 01:41
  • Sorry, I meant arguments. I looked up the doc online but couldn't seem to find the right method. I will continue to hunt. Thank you! – imaginov Nov 13 '17 at 02:17