title: How to carry out constrained regression in R
body: <p>Say I have a simple regression equation </p>
  
  <p><code>lm(y~., newdata=df)</code></p>
  
  <p>I know that if I want to reduce the intercept to 0, I write </p>
  
  <p><code>lm(y+0., newdata=df)</code></p>
  
  <p>However, is there a way to produce a step regression, all while constraining each coefficient to a specific range? For instance:</p>
  
  <p><code>step(lm(y~.&gt;1000, newdata=df)</code></p>
  
  <p>The above does not work, but is there a way to say produce a regression that basically produces the best fit and forces each coefficient to be greater than 1,000? Or, less than a specified range.</p>
  
  <pre><code>#as per Gautam    
  minfunc &lt;- function(coefs){
        out &lt;- sum(sapply(3:314, function(z) return(coefs[z]*test2[, z])))
        return(out)
      }
  
  
      par = c(1, 1, 30) # initial value
      lb = c(-1, -1, -300000) # lower bound for coefs
      ub = c(30, 30, 30000) # upper bound 
  
      result &lt;- hjkb(par = par, fn = minfunc, lower = lb, upper = ub)
  </code></pre>
  
  <p>Thank you,</p>