title: Add a multiple polynomial regression plane to a 3D scatter plot using Plotly
body: <p>I already developed the multiple regression equation and know the coefficients, and have used plotly to graph both a fitted surface and a scatter plot of my data. What I want to do now is take the regression model and fit it as a 3D plane with the scatter plot. I tried using the code from similar questions that have been asked, but have not had any luck getting it to work. See these below:</p>
  <p><a href="https://stackoverflow.com/questions/38331198/add-regression-plane-to-3d-scatter-plot-in-plotly">Add Regression Plane to 3d Scatter Plot in Plotly</a></p>
  <p><a href="https://stackoverflow.com/questions/57908472/configuring-regression-plane-added-to-3d-scatter-plot-in-plotly">Configuring Regression Plane added to 3d Scatter Plot in Plotly</a></p>
  <p>Here is what I have so far:</p>
  <pre><code>data &lt;-read_xlsx
  x1 &lt;- data$Temperature
  x2 &lt;- data$Days
  fit &lt;- lm(Release ~ poly(x1, 2, raw = TRUE) + poly(x2, 2, raw = TRUE), data = data)
  summary(fit)
  #Use the summary to obtain coefficients
  
  library(plotly)
      x &lt;- x1
      y &lt;- x2
      z &lt;- data$Release
  
  fig &lt;- plot_ly(data, x = ~x, y = ~y, z = ~z, color = ~z)
  fig &lt;- fig %&gt;% layout(
      title = &quot;CRF N Release Over Temperatures and Time&quot;,
      scene = list(
        xaxis = list(title = &quot;Temperature (C)&quot;),
        yaxis = list(title = &quot;Time (Days)&quot;),
        zaxis = list(title = &quot;%N Release&quot;)
  </code></pre>
  <p>This all works great and produces a scatter plot (View Here: <a href="https://rpubs.com/jbhenry/799959" rel="nofollow noreferrer">https://rpubs.com/jbhenry/799959</a>). From here on is where the code just doesn't seem to work. I only got it to produce a 3D plane once, but it looked bizarre (View Here: <a href="https://rpubs.com/jbhenry/799963" rel="nofollow noreferrer">https://rpubs.com/jbhenry/799963</a>).</p>
  <pre><code>fit &lt;- lm(Release ~ poly(x1, 2, raw = TRUE) + poly(x2, 2, raw = TRUE), data = data)
  
  #Graph Resolution (more important for more complex shapes)
  graph_reso &lt;- 1
  
  #Setup Axis
  axis_x &lt;- seq(min(data$Temperature), max(data$Temperature), by = graph_reso)
  axis_y &lt;- seq(min(data$Days), max(data$Days), by = graph_reso)
  
  library(reshape2)
  
  #Sample points
  crf_lm_surface &lt;- expand.grid(Temperature = axis_x, Days = axis_y,KEEP.OUT.ATTRS = F)
  crf_lm_surface$release &lt;- predict.lm(fit, newdata = crf_lm_surface)
  crf_lm_surface &lt;- acast(crf_lm_surface, Temperature ~ Days, value.var = &quot;release&quot;) #y ~ x
  
  fig &lt;- add_trace(p = fig,
                         z = crf_lm_surface,
                         x = axis_x,
                         y = axis_y,
                         type = &quot;surface&quot;)
  fig
  </code></pre>
  <p>The regression plane should not look anything like what is produced by this code. Can anyone help me figure out where things are going wrong?</p>