title: Is it possible to specify lower bound in response variable during smooth with gam?
body: <p>I am trying to fit a smoothed surface of <code>z</code> against <code>x</code> and <code>y</code> using formula <code>z ~ s(x, y)</code> with <code>gam</code> function
   in <code>mgcv</code> package. My goal is to predict response <code>z</code> based on new values of <code>x</code> and <code>y</code>.</p>
  
  <p>In my real situation, <code>z</code> should be a positive number negative <code>z</code> would be meaningless. However, the predicted <code>z</code>s
  are sometimes negative. It seems that for some region, there is not enough points in the training data to estimate <code>z</code>
  accurately.</p>
  
  <p>My question is: <strong>Is there a way to specifiy a lower bound of <code>z</code> during smooth in <code>gam</code> so that later I won't get negative <code>z</code>s with <code>predict</code>?</strong></p>
  
  <p>Below is a minimal example that reproduces this issue.</p>
  
  <pre><code>library(mgcv)
  
  x &lt;- seq(0.1, 1, by = 0.01)
  y &lt;- seq(0.1, 1, by = 0.01)
  dtt &lt;- expand.grid(x = x, y = y)
  
  set.seed(123)
  dtt$xp &lt;- dtt$x + rnorm(nrow(dtt)) / 100
  dtt$yp &lt;- dtt$y + rnorm(nrow(dtt)) / 100
  
  dtt$z &lt;- 1 / (dtt$xp^2 + dtt$yp^2)
  
  m &lt;- sample.int(nrow(dtt), 3000)
  
  dtt.train &lt;- dtt[m, ]
  dtt.test &lt;- dtt[!(1:nrow(dtt) %in% m), ]
  
  fit &lt;- gam(z ~ s(x, y), data = dtt.train)
  
  p &lt;- predict(fit, newdata = dtt.test)
  
  plot(dtt.test$z, p, xlab = 'Real', ylab = 'Predicted', pch = 19, col = 1 + (p &lt; 0))
  abline(h = 0, v = 0)
  </code></pre>
  
  <p>As you can see, for the red points. the real values are positive but the predicted values are negative.</p>
  
  <p><a href="https://i.stack.imgur.com/JcwlR.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JcwlR.png" alt="enter image description here"></a></p>