0

I am trying to use R to plot a function that takes the form:

$$z=\beta_{0}+\beta_{1}x+\beta_{2}x^{2}+\beta_{3}y+\beta_{4}y^{2}+\beta_{5}x*y$$

Where I have numerical values for all the $\beta$'s. I can't seem to find an example of using the the functional form of -scatter3d- and I am not running a regression within R nor plotting the series of z, x, and y.

I currently have

scatter3D(df$z=0.0279x-0.0000188x_sq+0.0422y-0.00708y_sq-0.000181x_y, df)

And haven't even added anything else to it because I get an error message:

Error: unexpected '=' in "scatter3D(df$z="

So I am assuming it is a syntax problem but can't seem to find anything about it.

Brennan
  • 419
  • 5
  • 17

1 Answers1

0

A couple of things:

You won't be able to plot 4 continuous variables against a single dependent on only 2 axes.

Formulas follow the pattern y ~ x + z so change your = to a ~.

You don't need a df$ in front of any variable since you specify the data with the ..., df part.

Formula's don't accept coefficients but you can easily change them in the original data frame like this:

df<-data.frame(x=rnorm(100,5,1),
           y=rnorm(100,50,1),
           z=rnorm(100,500,1))

df$x_sq<-0.000188*(df$x)^2
df$y_sq<-0.0708*(df$y)^2
  df$y2<-0.0422*(df$y)
  df$x2<-0.0279*(df$x)

   scatter3d(z~x2+y2,df)

Also https://meta.stackexchange.com/questions/30559/latex-on-stack-overflow

If you want a surface, not a scatter, then modify coefficients in this:

  x <- seq(-10, 10, length.out = 50)  
  y <- x 
  zf<-function(x,y){1*x^2+1*x-0.1*y^2-0.1*y+0.1*y*1*x}
  z <- outer(x, y, zf)
             surface3d(x,y,z)
CrunchyTopping
  • 803
  • 7
  • 17
  • How would I include the interaction effect? (i.e. the effect of x on z varies with different levels of y when looking at the first order partial derivative). Re: latex; that is sad – Brennan Aug 19 '19 at 20:12