<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/microeconomic-theory-and-linear-regression-part-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://pachamaltese.github.io/2017-04-22_microeconomic_theory_and_linear_regression_p2.html"> Pachá (Batteries Included)</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>Introduction</h1>
<p>In the <a target="_blank" rel="nofollow" href="http://pacha.hk/2017-04-11_microeconomic_theory_and_linear_regression.html">first part</a> of this article I explained and compared four different functional forms to model apples’ production.</p>
<p>This is what I need to retake the previously explained examples:</p>
<div>
<pre><span></span><span># Libraries</span>

<span>#install.packages(c(&quot;micEcon&quot;,&quot;lmtest&quot;,&quot;bbmle&quot;,&quot;miscTools&quot;))</span>
<span>library</span><span>(</span>micEcon<span>)</span>
<span>library</span><span>(</span>lmtest<span>)</span>
<span>library</span><span>(</span>stats4<span>)</span> <span>#this is a base package so I don't install this</span>
<span>library</span><span>(</span>bbmle<span>)</span>
<span>library</span><span>(</span>miscTools<span>)</span>

<span># Load data</span>

data<span>(</span><span>&quot;appleProdFr86&quot;</span><span>,</span> package <span>=</span> <span>&quot;micEcon&quot;</span><span>)</span>
data <span>=</span> appleProdFr86
<span>rm</span><span>(</span>appleProdFr86<span>)</span>

data<span>$</span>qCap <span>=</span> data<span>$</span>vCap<span>/</span>data<span>$</span>pCap
data<span>$</span>qLab <span>=</span> data<span>$</span>vLab<span>/</span>data<span>$</span>pLab
data<span>$</span>qMat <span>=</span> data<span>$</span>vMat<span>/</span>data<span>$</span>pMat

<span># Fit models</span>

<span>## Linear specification</span>
prodLin <span>=</span> lm<span>(</span>qOut <span>~</span> qCap <span>+</span> qLab <span>+</span> qMat<span>,</span> data <span>=</span> data<span>)</span>
data<span>$</span>qOutLin <span>=</span> fitted<span>(</span>prodLin<span>)</span>

<span>## Quadratic specification</span>
prodQuad <span>=</span> lm<span>(</span>qOut <span>~</span> qCap <span>+</span> qLab <span>+</span> qMat <span>+</span> <span>I</span><span>(</span><span>0.5</span><span>*</span>qCap<span>^</span><span>2</span><span>)</span> <span>+</span> <span>I</span><span>(</span><span>0.5</span><span>*</span>qLab<span>^</span><span>2</span><span>)</span> <span>+</span> <span>I</span><span>(</span><span>0.5</span><span>*</span>qMat<span>^</span><span>2</span><span>)</span>
               <span>+</span> <span>I</span><span>(</span>qCap<span>*</span>qLab<span>)</span> <span>+</span> <span>I</span><span>(</span>qCap<span>*</span>qMat<span>)</span> <span>+</span> <span>I</span><span>(</span>qLab<span>*</span>qMat<span>),</span> data <span>=</span> data<span>)</span>
data<span>$</span>qOutQuad <span>=</span> fitted<span>(</span>prodQuad<span>)</span>

<span>## Cobb-Douglas specification</span>
prodCD <span>=</span> lm<span>(</span><span>log</span><span>(</span>qOut<span>)</span> <span>~</span> <span>log</span><span>(</span>qCap<span>)</span> <span>+</span> <span>log</span><span>(</span>qLab<span>)</span> <span>+</span> <span>log</span><span>(</span>qMat<span>),</span> data <span>=</span> data<span>)</span>
data<span>$</span>qOutCD <span>=</span> fitted<span>(</span>prodCD<span>)</span>

<span>## Translog specification</span>
prodTL <span>=</span> lm<span>(</span><span>log</span><span>(</span>qOut<span>)</span> <span>~</span> <span>log</span><span>(</span>qCap<span>)</span> <span>+</span> <span>log</span><span>(</span>qLab<span>)</span> <span>+</span> <span>log</span><span>(</span>qMat<span>)</span> <span>+</span> <span>I</span><span>(</span><span>0.5</span><span>*</span><span>log</span><span>(</span>qCap<span>)</span><span>^</span><span>2</span><span>)</span> 
             <span>+</span> <span>I</span><span>(</span><span>0.5</span><span>*</span><span>log</span><span>(</span>qLab<span>)</span><span>^</span><span>2</span><span>)</span> <span>+</span> <span>I</span><span>(</span><span>0.5</span><span>*</span><span>log</span><span>(</span>qMat<span>)</span><span>^</span><span>2</span><span>)</span> <span>+</span> <span>I</span><span>(</span><span>log</span><span>(</span>qCap<span>)</span><span>*</span><span>log</span><span>(</span>qLab<span>))</span>
             <span>+</span> <span>I</span><span>(</span><span>log</span><span>(</span>qCap<span>)</span><span>*</span><span>log</span><span>(</span>qMat<span>))</span> <span>+</span> <span>I</span><span>(</span><span>log</span><span>(</span>qLab<span>)</span><span>*</span><span>log</span><span>(</span>qMat<span>)),</span> data <span>=</span> data<span>)</span>
data<span>$</span>qOutTL <span>=</span> fitted<span>(</span>prodTL<span>)</span>
</pre>
</div>
<p>These four specifications presented different problems that suggested model misspecification. Ideally, I should find a model with positive predicted output and positive factor elasticity to make sense of the context of production I’m working with.</p>
<h1>Model misspectification</h1>
<p>RESET test (the name stands for <em>Regression Equation Specification Error Test</em>) is a test for functional form misspecification.</p>
<p>As a general test, RESET helps detecting ommited variables. Package <code>lmtest</code> provides <code>resettest</code>, a function that provides one of the many versions of the test, and it fits this model by default:
</p>
<div>$$y_i = \beta_0 + \beta_1 x_1 + \ldots + \beta_p x_p + \gamma_1 \hat{y}_i^2 + \gamma_2 \hat{y}_i^3 + e_i$$</div>
<p>
The key idea behind fitting another model that includes the output is to detect if the model ignores an important nonlinearity.</p>
<p>The hypothesis testing is:
</p>
<div>$$H_0:\: \gamma_i = 0 \text{ for any } i \\<br>
H_1:\: \gamma_i \neq 0 \text{ for some } i$$</div>
<p>If there is evidence to reject the null hypthosis, the test does not provide an alternative model and it just states that the model is misspecified.</p>
<p>The statistic will be a <em>t</em> statistic if we just include <span>\(\gamma_1 \hat{y}_i^2\)</span>, and it will change to a <em>F</em> statistic if we include <span>\(\gamma_2 \hat{y}_i^3\)</span> or a higher power. </p>
<p>In the context of the worked specifications:</p>
<div>
<pre><span></span>resettest<span>(</span>prodLin<span>)</span>
</pre>
</div>
<div>
<pre><span></span>    RESET test

data:  prodLin
RESET = 17.639, df1 = 2, df2 = 134, p-value = 1.584e-07
</pre>
</div>
<div>
<pre><span></span>resettest<span>(</span>prodQuad<span>)</span>
</pre>
</div>
<div>
<pre><span></span>    RESET test

data:  prodQuad
RESET = 7.3663, df1 = 2, df2 = 128, p-value = 0.0009374
</pre>
</div>
<div>
<pre><span></span>resettest<span>(</span>prodCD<span>)</span>
</pre>
</div>
<div>
<pre><span></span>    RESET test

data:  prodCD
RESET = 2.9224, df1 = 2, df2 = 134, p-value = 0.05724
</pre>
</div>
<div>
<pre><span></span>resettest<span>(</span>prodTL<span>)</span>
</pre>
</div>
<div>
<pre><span></span>    RESET test

data:  prodTL
RESET = 1.2811, df1 = 2, df2 = 128, p-value = 0.2813
</pre>
</div>
<p><span>\(\Longrightarrow\)</span> The null hypothesis is rejected both for Linear and Quadratic specifications while Cobb-Douglas specification is rejected at 10% of significance. The null hypothesis can’t be rejected for the Translog Specification.</p>
<p>I can also modify the powers in the model:</p>
<div>
<pre><span></span>resettest<span>(</span>prodTL<span>,</span> power <span>=</span> <span>2</span><span>)</span>
</pre>
</div>
<div>
<pre><span></span>    RESET test

data:  prodTL
RESET = 2.358, df1 = 1, df2 = 129, p-value = 0.1271
</pre>
</div>
<div>
<pre><span></span>resettest<span>(</span>prodTL<span>,</span> power <span>=</span> <span>2</span><span>:</span><span>4</span><span>)</span>
</pre>
</div>
<div>
<pre><span></span>    RESET test

data:  prodTL
RESET = 1.3475, df1 = 3, df2 = 127, p-value = 0.262
</pre>
</div>
<p><span>\(\Longrightarrow\)</span> The null hypothesis can’t be rejected for Translog Specification, not even by adding a higher power.</p>
<h1>Quasiconcavity</h1>
<p>A function
</p>
<div>$$<br>
\begin{align*}<br>
f: \mathbb{R}^n &amp;\to \mathbb{R} \cr<br>
x &amp;\mapsto f(x)<br>
\end{align*}<br>
$$</div>
<p>
is quasiconcave if for <span>\(0 \leq t \leq 1\)</span>
</p>
<div>$$f(tx_1 + (1-t)x_2) \geq \min\{f(x_1),f(x_2)\}$$</div>
<p>In particular, any concave and twice continuously differentiable function is quasiconcave.</p>
<p>In really raw terms, quasiconcavity guarantees there is a production plan where the company is minimizing cost. Adding differentiability, you can find an optimal production plan by using derivatives.</p>
<p>To cehck the details please check <a target="_blank" rel="nofollow" href="https://www.amazon.com/Advanced-Microeconomic-Theory-Geoffrey-Jehle/dp/0273731912">Advanced Microeconomic Theory by Geoffrey A. Jehle</a>.</p>
<p>I’ll check the quasiconcavity in an indirect way, by checking the concavity at each observation using the hessian method:</p>
<div>
<pre><span></span><span># Quadratic Specification</span>

<span>## Regression coefficients</span>
b1  <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;qCap&quot;</span><span>]</span>
b2  <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;qLab&quot;</span><span>]</span>
b3  <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;qMat&quot;</span><span>]</span>
b11 <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;I(0.5 * qCap^2)&quot;</span><span>]</span>
b22 <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;I(0.5 * qLab^2)&quot;</span><span>]</span>
b33 <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;I(0.5 * qMat^2)&quot;</span><span>]</span>
b12 <span>=</span> b21 <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;I(qCap * qLab)&quot;</span><span>]</span>
b13 <span>=</span> b31 <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;I(qCap * qMat)&quot;</span><span>]</span>
b23 <span>=</span> b32 <span>=</span> coef<span>(</span>prodQuad<span>)[</span><span>&quot;I(qLab * qMat)&quot;</span><span>]</span>

<span>## Marginal productivity</span>
data<span>$</span>mpCapQuad <span>=</span> <span>with</span><span>(</span>data<span>,</span> b1 <span>+</span> b11<span>*</span>qCap <span>+</span> b12<span>*</span>qLab <span>+</span> b13<span>*</span>qMat<span>)</span>
data<span>$</span>mpLabQuad <span>=</span> <span>with</span><span>(</span>data<span>,</span> b2 <span>+</span> b21<span>*</span>qCap <span>+</span> b22<span>*</span>qLab <span>+</span> b23<span>*</span>qMat<span>)</span>
data<span>$</span>mpMatQuad <span>=</span> <span>with</span><span>(</span>data<span>,</span> b3 <span>+</span> b31<span>*</span>qCap <span>+</span> b32<span>*</span>qLab <span>+</span> b33<span>*</span>qMat<span>)</span>

<span>## Quasiconcavity</span>
data<span>$</span>quasiConcQuad <span>=</span> <span>NA</span>

<span>for</span><span>(</span>obs <span>in</span> <span>1</span><span>:</span><span>nrow</span><span>(</span>data<span>))</span> <span>{</span>
  hmLoop <span>=</span> <span>matrix</span><span>(</span><span>0</span><span>,</span> nrow <span>=</span> <span>3</span><span>,</span> ncol <span>=</span> <span>3</span><span>)</span>
  hmLoop<span>[</span><span>1</span><span>,</span><span>1</span><span>]</span> <span>=</span> b11
  hmLoop<span>[</span><span>1</span><span>,</span><span>2</span><span>]</span> <span>=</span> hmLoop<span>[</span><span>2</span><span>,</span><span>1</span><span>]</span> <span>=</span> b12
  hmLoop<span>[</span><span>1</span><span>,</span><span>3</span><span>]</span> <span>=</span> hmLoop<span>[</span><span>3</span><span>,</span><span>1</span><span>]</span> <span>=</span> b13
  hmLoop<span>[</span><span>2</span><span>,</span><span>2</span><span>]</span> <span>=</span> b22
  hmLoop<span>[</span><span>2</span><span>,</span><span>3</span><span>]</span> <span>=</span> hmLoop<span>[</span><span>3</span><span>,</span><span>2</span><span>]</span> <span>=</span> b23
  hmLoop<span>[</span><span>3</span><span>,</span><span>3</span><span>]</span> <span>=</span> b33

  data<span>$</span>quasiConcQuad<span>[</span>obs<span>]</span> <span>=</span> hmLoop<span>[</span><span>1</span><span>,</span><span>1</span><span>]</span> <span>&lt;</span> <span>0</span> <span>&amp;</span> <span>det</span><span>(</span>hmLoop<span>[</span><span>1</span><span>:</span><span>2</span><span>,</span><span>1</span><span>:</span><span>2</span><span>]</span> <span>></span> <span>0</span><span>)</span> <span>&amp;</span> <span>det</span><span>(</span>hmLoop <span>&lt;</span> <span>0</span><span>)</span>
<span>}</span>

<span>sum</span><span>(</span><span>!</span>data<span>$</span>quasiConcQuad<span>)</span>
</pre>
</div>
<div>
<pre><span></span>[1] 140
</pre>
</div>
<div>
<pre><span></span><span># Translog Specification</span>

<span>## Regression coefficients</span>
b1  <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;log(qCap)&quot;</span><span>]</span>
b2  <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;log(qLab)&quot;</span><span>]</span>
b3  <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;log(qMat)&quot;</span><span>]</span>
b11 <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;I(0.5 * log(qCap)^2)&quot;</span><span>]</span>
b22 <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;I(0.5 * log(qLab)^2)&quot;</span><span>]</span>
b33 <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;I(0.5 * log(qMat)^2)&quot;</span><span>]</span>
b12 <span>=</span> b21 <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;I(log(qCap) * log(qLab))&quot;</span><span>]</span>
b13 <span>=</span> b31 <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;I(log(qCap) * log(qMat))&quot;</span><span>]</span>
b23 <span>=</span> b32 <span>=</span> coef<span>(</span>prodTL<span>)[</span><span>&quot;I(log(qLab) * log(qMat))&quot;</span><span>]</span> 

<span>## Scale elasticity</span>
data<span>$</span>eCapTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> b1 <span>+</span> b11<span>*</span><span>log</span><span>(</span>qCap<span>)</span> <span>+</span> b12<span>*</span><span>log</span><span>(</span>qLab<span>)</span> <span>+</span> b13<span>*</span><span>log</span><span>(</span>qMat<span>))</span>
data<span>$</span>eLabTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> b2 <span>+</span> b21<span>*</span><span>log</span><span>(</span>qCap<span>)</span> <span>+</span> b22<span>*</span><span>log</span><span>(</span>qLab<span>)</span> <span>+</span> b23<span>*</span><span>log</span><span>(</span>qMat<span>))</span>
data<span>$</span>eMatTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> b3 <span>+</span> b31<span>*</span><span>log</span><span>(</span>qCap<span>)</span> <span>+</span> b32<span>*</span><span>log</span><span>(</span>qLab<span>)</span> <span>+</span> b33<span>*</span><span>log</span><span>(</span>qMat<span>))</span>

<span>## Marginal productivity</span>
data<span>$</span>mpCapTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> eCapTL <span>*</span> qOutTL <span>/</span> qCap<span>)</span>
data<span>$</span>mpLabTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> eLabTL <span>*</span> qOutTL <span>/</span> qLab<span>)</span>
data<span>$</span>mpMatTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> eMatTL <span>*</span> qOutTL <span>/</span> qMat<span>)</span>

<span>## Derivatives of marginal productivity</span>
data<span>$</span>dmpCapCapTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> <span>(</span>qOutTL <span>/</span> qCap<span>^</span><span>2</span><span>)</span> <span>*</span> <span>(</span>b11 <span>+</span> eCapTL<span>^</span><span>2</span> <span>-</span> eCapTL<span>))</span>
data<span>$</span>dmpLabLabTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> <span>(</span>qOutTL <span>/</span> qLab<span>^</span><span>2</span><span>)</span> <span>*</span> <span>(</span>b22 <span>+</span> eLabTL<span>^</span><span>2</span> <span>-</span> eLabTL<span>))</span>
data<span>$</span>dmpMatMatTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> <span>(</span>qOutTL <span>/</span> qLab<span>^</span><span>2</span><span>)</span> <span>*</span> <span>(</span>b33 <span>+</span> eMatTL<span>^</span><span>2</span> <span>-</span> eMatTL<span>))</span>

data<span>$</span>dmpCapLabTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> <span>(</span>qOutTL <span>/</span> <span>(</span>qCap <span>*</span> qLab<span>))</span> <span>*</span> <span>(</span>b12 <span>+</span> eCapTL <span>*</span> eLabTL<span>))</span>
data<span>$</span>dmpCapMatTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> <span>(</span>qOutTL <span>/</span> <span>(</span>qCap <span>*</span> qMat<span>))</span> <span>*</span> <span>(</span>b13 <span>+</span> eCapTL <span>*</span> eMatTL<span>))</span>

data<span>$</span>dmpLabMatTL <span>=</span> <span>with</span><span>(</span>data<span>,</span> <span>(</span>qOutTL <span>/</span> qLab <span>*</span> qMat<span>)</span> <span>*</span> <span>(</span>b23 <span>+</span> eLabTL <span>*</span> qMat<span>))</span>

<span>## Quasiconcavity</span>
data<span>$</span>quasiConcTL <span>=</span> <span>NA</span>

<span>for</span><span>(</span>obs <span>in</span> <span>1</span><span>:</span><span>nrow</span><span>(</span>data<span>))</span> <span>{</span>
  hmLoop <span>=</span> <span>matrix</span><span>(</span><span>0</span><span>,</span> nrow <span>=</span> <span>3</span><span>,</span> ncol <span>=</span> <span>3</span><span>)</span>
  hmLoop<span>[</span><span>1</span><span>,</span><span>1</span><span>]</span> <span>=</span> data<span>$</span>dmpCapCapTL<span>[</span>obs<span>]</span>
  hmLoop<span>[</span><span>1</span><span>,</span><span>2</span><span>]</span> <span>=</span> hmLoop<span>[</span><span>2</span><span>,</span><span>1</span><span>]</span> <span>=</span> data<span>$</span>dmpCapLabTL<span>[</span>obs<span>]</span>
  hmLoop<span>[</span><span>1</span><span>,</span><span>3</span><span>]</span> <span>=</span> hmLoop<span>[</span><span>3</span><span>,</span><span>1</span><span>]</span> <span>=</span> data<span>$</span>dmpCapMatTL<span>[</span>obs<span>]</span>
  hmLoop<span>[</span><span>2</span><span>,</span><span>2</span><span>]</span> <span>=</span> data<span>$</span>dmpLabLabTL<span>[</span>obs<span>]</span>
  hmLoop<span>[</span><span>2</span><span>,</span><span>3</span><span>]</span> <span>=</span> hmLoop<span>[</span><span>3</span><span>,</span><span>2</span><span>]</span> <span>=</span> data<span>$</span>dmpLabMatTL<span>[</span>obs<span>]</span>
  hmLoop<span>[</span><span>3</span><span>,</span><span>3</span><span>]</span> <span>=</span> data<span>$</span>dmpMatMatTL<span>[</span>obs<span>]</span>

  data<span>$</span>quasiConcTL<span>[</span>obs<span>]</span> <span>=</span> hmLoop<span>[</span><span>1</span><span>,</span><span>1</span><span>]</span> <span>&lt;</span> <span>0</span> <span>&amp;</span> <span>det</span><span>(</span>hmLoop<span>[</span><span>1</span><span>:</span><span>2</span><span>,</span><span>1</span><span>:</span><span>2</span><span>]</span> <span>></span> <span>0</span><span>)</span> <span>&amp;</span> <span>det</span><span>(</span>hmLoop <span>&lt;</span> <span>0</span><span>)</span>
<span>}</span>

<span>sum</span><span>(</span><span>!</span>data<span>$</span>quasiConcTL<span>)</span>
</pre>
</div>
<div>
<pre><span></span>[1] 126
</pre>
</div>
<p>With this information I can extend the information from the last table in the first part of the post:</p>
<table>
<thead>
<tr>
<th></th>
<th>Quadratic</th>
<th>Translog</th>
</tr>
</thead>
<tbody>
<tr>
<td><span>\(R^2\)</span> on <span>\(y\)</span></td>
<td>0.85</td>
<td>0.77</td>
</tr>
<tr>
<td><span>\(R^2\)</span> on <span>\(ln(y)\)</span></td>
<td>0.55</td>
<td>0.63</td>
</tr>
<tr>
<td>Obs. with negative predicted output</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>Obs. that violate the monotonicity condition</td>
<td>39</td>
<td>48</td>
</tr>
<tr>
<td>Obs. with negative scale elasticity</td>
<td>22</td>
<td>0</td>
</tr>
<tr>
<td>Obs. that violate the quasiconcavity condition</td>
<td>140</td>
<td>126</td>
</tr>
</tbody>
</table>
<p><span>\(\Longrightarrow\)</span> Now I have more evidence in favour of a different approach as it was mentioned at the end of the first part of the post.</p>
<h1>Non-parametric regression</h1>
<p>A non-parametric model is of the form:
</p>
<div>$$Y_i = m(X_i) + \varepsilon_i$$</div>
<p>The advantages are:<br>
<em> The functional form is not defined beforehand<br>
</em> Obtains a best fit function <span>\(m\)</span> from the data<br>
* Obtains marginal effects</p>
<p>The disadvantages are:<br>
<em> Higher computational cost<br>
</em> There are no regression coefficients (the focus is the regression function)<br>
* Hard to interpret</p>
<h2>Single variable non-parametric regression</h2>
<p>I can predict the output by using capital as the only input:</p>
<div>
<pre><span></span><span>library</span><span>(</span>KernSmooth<span>)</span>

qCap <span>=</span> <span>as.vector</span><span>(</span>data<span>$</span>qCap<span>)</span>
qOut <span>=</span> <span>as.vector</span><span>(</span>data<span>$</span>qOut<span>)</span>

bw <span>=</span> dpill<span>(</span>qCap<span>,</span>qOut<span>)</span>
fit1 <span>=</span> locpoly<span>(</span>qCap<span>,</span> qOut<span>,</span> bandwidth <span>=</span> bw<span>,</span> degree <span>=</span> <span>1</span><span>)</span> <span># 1st degree polynomial</span>
plot<span>(</span>fit1<span>$</span>x<span>,</span> fit1<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>)</span>
</pre>
</div>
<p><img title="plot of chunk microeconomics_6" alt="plot of chunk microeconomics_6" src="http://pachamaltese.github.io/images/2017-04-22_microeconomic_theory_and_linear_regression_p2/microeconomics_6-1.svg"></p>
<p>Here are different specifications:</p>
<div>
<pre><span></span><span># 2nd degree polynomial</span>
fit2.1 <span>&lt;-</span> locpoly<span>(</span>qCap<span>,</span> qOut<span>,</span> bandwidth <span>=</span> bw<span>,</span> degree <span>=</span> <span>2</span><span>)</span>
fit2.2 <span>&lt;-</span> locpoly<span>(</span>qCap<span>,</span> qOut<span>,</span> bandwidth <span>=</span> bw<span>*</span><span>2</span><span>,</span> degree <span>=</span> <span>2</span><span>)</span> <span># less variance / more bias</span>
fit2.3 <span>&lt;-</span> locpoly<span>(</span>qCap<span>,</span> qOut<span>,</span> bandwidth <span>=</span> bw<span>/</span><span>2</span><span>,</span> degree <span>=</span> <span>2</span><span>)</span> <span># more variance / less bias</span>

<span># linear regression</span>
fit.lm <span>&lt;-</span> lm<span>(</span>qOut <span>~</span> qCap<span>)</span>

<span># plots</span>
plot<span>(</span>fit1<span>$</span>x<span>,</span> fit1<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>);</span>par<span>(</span>mfrow<span>=</span><span>c</span><span>(</span><span>2</span><span>,</span><span>2</span><span>))</span>
plot<span>(</span>fit1<span>$</span>x<span>,</span> fit1<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>);</span>lines<span>(</span>fit2.1<span>,</span> col <span>=</span> <span>&quot;blue&quot;</span><span>)</span>
plot<span>(</span>fit1<span>$</span>x<span>,</span> fit1<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>);</span>lines<span>(</span>fit2.2<span>,</span> col <span>=</span> <span>&quot;red&quot;</span><span>)</span>
plot<span>(</span>fit1<span>$</span>x<span>,</span> fit1<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>);</span>lines<span>(</span>fit2.3<span>,</span> col <span>=</span> <span>&quot;green&quot;</span><span>)</span>
plot<span>(</span>fit1<span>$</span>x<span>,</span> fit1<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>);</span>abline<span>(</span>fit.lm<span>,</span> col <span>=</span> <span>&quot;purple&quot;</span><span>)</span>
</pre>
</div>
<p><img title="plot of chunk microeconomics_7" alt="plot of chunk microeconomics_7" src="http://pachamaltese.github.io/images/2017-04-22_microeconomic_theory_and_linear_regression_p2/microeconomics_7-1.svg"></p>
<p>Checking the models:</p>
<div>
<pre><span></span><span># model 1</span>
<span>sum</span><span>(</span><span>is.na</span><span>(</span>fit1<span>$</span>y<span>))</span>
</pre>
</div>
<div>
<pre><span></span>[1] 50
</pre>
</div>
<div>
<pre><span></span><span># models 2, 3 y 4</span>
<span>sum</span><span>(</span><span>is.na</span><span>(</span>fit2.1<span>$</span>y<span>))</span>
</pre>
</div>
<div>
<pre><span></span>[1] 50
</pre>
</div>
<div>
<pre><span></span><span>sum</span><span>(</span><span>is.na</span><span>(</span>fit2.2<span>$</span>y<span>))</span>
</pre>
</div>
<div>
<pre><span></span>[1] 9
</pre>
</div>
<div>
<pre><span></span><span>sum</span><span>(</span><span>is.na</span><span>(</span>fit2.3<span>$</span>y<span>))</span>
</pre>
</div>
<div>
<pre><span></span>[1] 84
</pre>
</div>
<p><span>\(\Longrightarrow\)</span> I should use model 2.2 and adjust it.</p>
<p>I’ll try with a neoclassical function with diminishing marginal returns:</p>
<div>
<pre><span></span><span># se ajusta la banda y el grado</span>
fit.sqrt <span>=</span> locpoly<span>(</span>qCap<span>,</span> qOut<span>,</span> bandwidth <span>=</span> bw<span>*</span><span>3</span><span>,</span> degree <span>=</span> <span>0.5</span><span>)</span>
<span>sum</span><span>(</span><span>is.na</span><span>(</span>fit.sqrt<span>$</span>y<span>))</span> 
</pre>
</div>
<div>
<pre><span></span>[1] 0
</pre>
</div>
<div>
<pre><span></span><span>sum</span><span>(</span>fit.sqrt<span>$</span>y <span>&lt;</span> <span>0</span><span>)</span>
</pre>
</div>
<div>
<pre><span></span>[1] 0
</pre>
</div>
<p><span>\(\Longrightarrow\)</span> In this model there is no indetermined or negative predicted output.</p>
<div>
<pre><span></span>plot<span>(</span>fit.sqrt<span>$</span>x<span>,</span> fit.sqrt<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>)</span>
</pre>
</div>
<p><img title="plot of chunk microeconomics_10" alt="plot of chunk microeconomics_10" src="https://i0.wp.com/pachamaltese.github.io/images/2017-04-22_microeconomic_theory_and_linear_regression_p2/microeconomics_10-1.png?w=456" data-recalc-dims="1"></p>
<p>I can obtain the median productivity:</p>
<div>
<pre><span></span>median<span>(</span>fit.sqrt<span>$</span>y<span>)</span><span>/</span>median<span>(</span>fit.sqrt<span>$</span>x<span>)</span>
</pre>
</div>
<div>
<pre><span></span>[1] 18.05637
</pre>
</div>
<p><span>\(\Longrightarrow\)</span> In median term each capital unit increases the output in 18 units.</p>
<p>If I adjust the bandwidth the function will look more and more like a neoclassical function from textbooks, with an initial part of increasing marginal returns and from the point where marginal and median productivities are equal (the same point where marginal productivity is at maximum) the return becomes increasing at decreasing scale.</p>
<p>This is an example of the last paragraph:</p>
<div>
<pre><span></span>fit.sqrt.2 <span>=</span> locpoly<span>(</span>qCap<span>,</span> qOut<span>,</span> bandwidth <span>=</span> bw<span>*</span><span>5</span><span>,</span> degree <span>=</span> <span>0.5</span><span>)</span>
plot<span>(</span>fit.sqrt.2<span>$</span>x<span>,</span> fit.sqrt.2<span>$</span>y<span>,</span> type <span>=</span> <span>&quot;l&quot;</span><span>)</span>
</pre>
</div>
<p><img title="plot of chunk microeconomics_12" alt="plot of chunk microeconomics_12" src="http://pachamaltese.github.io/images/2017-04-22_microeconomic_theory_and_linear_regression_p2/microeconomics_12-1.svg"></p>
<h2>Multivariable non-parametric regression</h2>
<p>Among many alternatives, <em>Epanechnikov’s method</em> is a non-parametric method that weights the contribution of each observation and iterating obtains the best fit function. </p>
<p>I’ll fit a regression applying logs to the variables:</p>
<div>
<pre><span></span><span>library</span><span>(</span>np<span>)</span>
<span>options</span><span>(</span>np.messages<span>=</span><span>FALSE</span><span>)</span>

prodNP <span>=</span> npreg<span>(</span><span>log</span><span>(</span>qOut<span>)</span> <span>~</span> <span>log</span><span>(</span>qCap<span>)</span> <span>+</span> <span>log</span><span>(</span>qLab<span>)</span> <span>+</span> <span>log</span><span>(</span>qMat<span>),</span> regtype <span>=</span> <span>&quot;ll&quot;</span><span>,</span> 
                bwmethod <span>=</span> <span>&quot;cv.aic&quot;</span><span>,</span> ckertype <span>=</span> <span>&quot;epanechnikov&quot;</span><span>,</span>  data <span>=</span> data<span>,</span> 
                gradients <span>=</span> <span>TRUE</span><span>)</span>
<span>summary</span><span>(</span>prodNP<span>)</span>
</pre>
</div>
<div>
<pre><span></span>Regression Data: 140 training points, in 3 variable(s)
              log(qCap) log(qLab) log(qMat)
Bandwidth(s):  1.039647    332644 0.8418465

Kernel Regression Estimator: Local-Linear
Bandwidth Type: Fixed
Residual standard error: 0.6227669
R-squared: 0.6237078

Continuous Kernel Type: Second-Order Epanechnikov
No. Continuous Explanatory Vars.: 3
</pre>
</div>
<p>These are the regression plots:<br>
<img title="plot of chunk microeconomics_14" alt="plot of chunk microeconomics_14" src="http://pachamaltese.github.io/images/2017-04-22_microeconomic_theory_and_linear_regression_p2/microeconomics_14-1.svg"><br>
the variables that do not appear in a plot are considered to be fixed to a value equal to their median.</p>
<p>I can test the significance for this model:</p>
<div>
<pre><span></span><span>options</span><span>(</span>np.messages<span>=</span><span>FALSE</span><span>)</span>
npsigtest<span>(</span>prodNP<span>)</span>
</pre>
</div>
<div>
<pre><span></span>Kernel Regression Significance Test
Type I Test with IID Bootstrap (399 replications, Pivot = TRUE, joint = FALSE)
Explanatory variables tested for significance:
log(qCap) (1), log(qLab) (2), log(qMat) (3)

              log(qCap) log(qLab) log(qMat)
Bandwidth(s):  1.039647    332644 0.8418465

Individual Significance Tests
P Value: 
log(qCap) 0.11779  
log(qLab) &lt; 2e-16 *** 
log(qMat) &lt; 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
</pre>
</div>
<p><span>\(\Longrightarrow\)</span> at 10% of significance the only variable with non-statistical significant effects is capital.</p>
<p>Finally I can obtain information about Product-Factor Elasticity and Scale Elasticity:</p>
<div>
<pre><span></span><span>summary</span><span>(</span>gradients<span>(</span>prodNP<span>)[,</span><span>1</span><span>])</span> <span># capital elasticity</span>
</pre>
</div>
<div>
<pre><span></span>    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-0.10300  0.08598  0.14360  0.14500  0.18810  0.30060 
</pre>
</div>
<div>
<pre><span></span><span>summary</span><span>(</span>gradients<span>(</span>prodNP<span>)[,</span><span>2</span><span>])</span> <span># labour elasticity</span>
</pre>
</div>
<div>
<pre><span></span>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.02208 0.63050 0.69880 0.70970 0.80670 0.97890 
</pre>
</div>
<div>
<pre><span></span><span>summary</span><span>(</span>gradients<span>(</span>prodNP<span>)[,</span><span>3</span><span>])</span> <span># materials elasticity</span>
</pre>
</div>
<div>
<pre><span></span>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.3873  0.4546  0.5669  0.5850  0.6502  1.3480 
</pre>
</div>
<div>
<pre><span></span><span>summary</span><span>(</span><span>rowSums</span><span>(</span>gradients<span>(</span>prodNP<span>)))</span> <span># scale elasticity</span>
</pre>
</div>
<div>
<pre><span></span>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.267   1.363   1.416   1.440   1.479   1.880 
</pre>
</div>
<div>
<pre><span></span>par<span>(</span>mfrow<span>=</span><span>c</span><span>(</span><span>2</span><span>,</span><span>2</span><span>))</span>
hist<span>(</span>gradients<span>(</span>prodNP<span>)[,</span><span>1</span><span>],</span> main <span>=</span> <span>&quot;Capital&quot;</span><span>)</span>
hist<span>(</span>gradients<span>(</span>prodNP<span>)[,</span><span>2</span><span>],</span> main <span>=</span> <span>&quot;Labour&quot;</span><span>)</span>
hist<span>(</span>gradients<span>(</span>prodNP<span>)[,</span><span>3</span><span>],</span> main <span>=</span> <span>&quot;Materials&quot;</span><span>)</span>
hist<span>(</span><span>rowSums</span><span>(</span>gradients<span>(</span>prodNP<span>)),</span> main <span>=</span> <span>&quot;Scale&quot;</span><span>)</span>
</pre>
</div>
<p><img title="plot of chunk microeconomics_16" alt="plot of chunk microeconomics_16" src="http://pachamaltese.github.io/images/2017-04-22_microeconomic_theory_and_linear_regression_p2/microeconomics_16-1.svg"></p>
<p></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://pachamaltese.github.io/2017-04-22_microeconomic_theory_and_linear_regression_p2.html"> Pachá (Batteries Included)</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/tMnub1Tlols"></a></div>