<p>Following the <a href="https://cran.r-project.org/web/packages/prodest/prodest.pdf" rel="nofollow noreferrer">prodtest</a> manual they define omega as residuals from the model, that is</p>
<p><span class="math-container">$$\omega_{it} = y_{it} − (\alpha + w_{it} \beta  + k_{it}\gamma)=y_{it}-\hat{y}_{it}$$</span></p>
<p>estprod does not have any comprehensible documentation but one way how we can get the above would be to extract the coefficients from the model and then calculate <span class="math-container">$y-\hat{y}$</span> manually.</p>
<p>We can do that with the broom package:</p>
<pre><code>install.packages(&quot;broom&quot;)
require(broom) 
coefs&lt;-tidy(mod1)
# A tibble: 3 x 3
  statistic   bias std.error
      &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;
1     -3.71   4.77      2.35
2     10.5  -11.8       4.26
3      1.73  -2.14      1.64
</code></pre>
<p>the names are off but the first column gives the same coefficients as when I inspect the model with summary so they are correct.</p>
<p>Next we can just can just calculate <span class="math-container">$y-\hat{y}$</span> as</p>
<pre><code>omega_1 = (data<span class="math-container">$Y - data$</span>Labor*coefs<span class="math-container">$statistic[1] - data$</span>Capital*coefs<span class="math-container">$statistic[2] - data$</span>Materials*coefs$statistic[3])  
</code></pre>
<p>the code is inelegant but it should do the job.</p>
<p>As a side note it might be worth while considering to estimate the TFP without these packages. Normally packages are the R's biggest strength but in this case estprod does not have any good accompanying documentation or manual and the prodest does not have the possibility to add the gross output yet as in the link the devs mentioned. In cases like this it might be better to just program your own function.</p>

CC BY-SA 4.0
2020-08-14T22:53:45.730Z
Thank you very much for your contribution, it would be very good if you contribute to the elaboration of one of the packages. With regard to the development of the functions, I am still learning about TFP, so I have a little difficulty with the math behind it.
cdcarrion
CC BY-SA 4.0
2020-08-14T16:15:46.290Z
2020-08-14T16:15:46.290Z
1muflon1
<p>I'm trying to calculate TFP with the <code>estprod</code> package in R because this package allows me to calculate with Gross-Output (like with levinsohn petrin) but I have trouble getting <a href="https://rdrr.io/cran/estprod/src/R/ss_lp_gross.R" rel="nofollow noreferrer">omega</a></p>
<p>While the <code>prodest</code> package can calculate omega but I can't add <a href="https://github.com/GabrieleRovigatti/prodest/issues/10#issuecomment-493787109" rel="nofollow noreferrer">Gross-Output</a>.</p>
<p>Has anyone calculated the TFP in R?</p>
<p><strong>[updated]</strong> Reproducible example</p>
<pre><code>library(prodest)
library(estprod)

data = structure(list(Y = c(12.8268952320028, 12.8521337216858, 12.8297798993317, 12.6357098405348, 
                            12.4004643557221, 12.3600068590732, 12.826153741752, 12.8990697648315, 
                            12.8393196511736, 12.7619091581355), 
                      Labor = c(18.2268433780233, 18.1280743937883, 
                            16.2004953940248, 18.4010948157752, 16.1374389570964, 18.3902253199657, 
                            16.2359220634517, 16.2468898407345, 16.2720069307031, 16.2673373011924), 
                      Capital = c(12.1642906069818, 12.088450259469, 
                           12.0237634086603, 12.0750443461238, 12.0098651639831, 12.0601371534517, 
                           11.9679864443247, 11.9299321784366, 12.0016971178669, 12.1209493577051), 
                      Materials = c(18.1472736836291, 17.7735308597061, 
                          17.1908332586684, 18.3914858811768, 18.2747180360049, 16.9304018090212, 
                          18.4414110290762, 18.0188590654862, 18.1427756804037, 15.805209131618), 
                      id = c(1070101, 1070101, 1070101, 1070101, 
                              1070101, 1070101, 1070102, 1070102, 1070102, 1070102), 
                      year = c(2013,2014, 2015, 2016, 2017, 2018, 2013, 2014, 2015, 2016)), 
                 class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -10L))

# Y, Labor, Capital, Materials are in logarithms
mod1 = estprod::levinsohn_petrin(data,
                                 formula = Y ~ Labor | Capital | Materials, 
                                 id = &quot;id&quot;, 
                                 time = &quot;year&quot;, 
                                 reps = 20,
                                 gross = T) #estprod with Gross-Output = T

#how can i calculate omega with estprod?

mod2 = prodest::prodestLP(data<span class="math-container">$Y, # prodest does not have the parameter Gross-Output
             fX = data$</span>Labor,
                 sX = data<span class="math-container">$Capital, 
             pX = data$</span>Materials,
                 idvar = data<span class="math-container">$id,
             timevar = data$</span>year,
                 opt='optim',
                 exit = F,
                 tol = 1e-100)  

omega = prodest::omega(mod2) #with 'prodest' I can calculate omega
</code></pre>

CC BY-SA 4.0
2020-08-13T01:59:23.137Z
"Has anyone calculated the TFP in R?" Yes many people did. I am afraid that without providing at least a minimal working example and more details and some proper question it will be very difficult to offer any help with the problem you are having.
1muflon1
CC BY-SA 4.0
2020-08-13T03:12:03.967Z
My mistake, I've already corrected
cdcarrion
CC BY-SA 4.0
2020-08-14T02:09:24.233Z
I tried to use your example but portion of code that builds the data frame has some errors. Also it would be helpful if the variable names were in English in all likelihood you can just estimate the missing component from either command from the model itself but I don’t even know what variables you are putting in since the description is not in English
1muflon1
CC BY-SA 4.0
2020-08-14T03:10:13.517Z
I just fixed the variable names, what do you mean the df has errors? I ran the code and it did work.
cdcarrion
CC BY-SA 4.0
2020-08-14T14:42:40.190Z
this new example works I will have a closer look at your problem tonight or tomorrow evening
1muflon1
CC BY-SA 4.0
2020-08-14T14:47:27.900Z
I really appreciate your help
cdcarrion
<macroeconomics><econometrics><production-function><r>
Total factor productivity (TFP) estimation in R
cdcarrion
# Total factor productivity (TFP) estimation in R

(Asked by: username_0 on 2020-08-12)

I'm trying to calculate TFP with the `estprod` package in R because this package allows me to calculate with Gross-Output (like with levinsohn petrin) but I have trouble getting [omega](https://rdrr.io/cran/estprod/src/R/ss_lp_gross.R)

While the `prodest` package can calculate omega but I can't add [Gross-Output](https://github.com/GabrieleRovigatti/prodest/issues/10#issuecomment-493787109).

Has anyone calculated the TFP in R?

**[updated]** Reproducible example

```
library(prodest)
library(estprod)

data = structure(list(Y = c(12.8268952320028, 12.8521337216858, 12.8297798993317, 12.6357098405348, 
                            12.4004643557221, 12.3600068590732, 12.826153741752, 12.8990697648315, 
                            12.8393196511736, 12.7619091581355), 
                      Labor = c(18.2268433780233, 18.1280743937883, 
                            16.2004953940248, 18.4010948157752, 16.1374389570964, 18.3902253199657, 
                            16.2359220634517, 16.2468898407345, 16.2720069307031, 16.2673373011924), 
                      Capital = c(12.1642906069818, 12.088450259469, 
                           12.0237634086603, 12.0750443461238, 12.0098651639831, 12.0601371534517, 
                           11.9679864443247, 11.9299321784366, 12.0016971178669, 12.1209493577051), 
                      Materials = c(18.1472736836291, 17.7735308597061, 
                          17.1908332586684, 18.3914858811768, 18.2747180360049, 16.9304018090212, 
                          18.4414110290762, 18.0188590654862, 18.1427756804037, 15.805209131618), 
                      id = c(1070101, 1070101, 1070101, 1070101, 
                              1070101, 1070101, 1070102, 1070102, 1070102, 1070102), 
                      year = c(2013,2014, 2015, 2016, 2017, 2018, 2013, 2014, 2015, 2016)), 
                 class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L))

# Y, Labor, Capital, Materials are in logarithms
mod1 = estprod::levinsohn_petrin(data,
                                 formula = Y ~ Labor | Capital | Materials, 
                                 id = "id", 
                                 time = "year", 
                                 reps = 20,
                                 gross = T) #estprod with Gross-Output = T

#how can i calculate omega with estprod?

mod2 = prodest::prodestLP(data$Y, # prodest does not have the parameter Gross-Output
             fX = data$Labor,
                 sX = data$Capital, 
             pX = data$Materials,
                 idvar = data$id,
             timevar = data$year,
                 opt='optim',
                 exit = F,
                 tol = 1e-100)  

omega = prodest::omega(mod2) #with 'prodest' I can calculate omega

```

**Tags:** macroeconomics, econometrics, production-function, r

> **Comments:**
> **username_1**: "Has anyone calculated the TFP in R?" Yes many people did. I am afraid that without providing at least a minimal working example and more details and some proper question it will be very difficult to offer any help with the problem you are having.  
> **username_0**: My mistake, I've already corrected  
> **username_1**: I tried to use your example but portion of code that builds the data frame has some errors. Also it would be helpful if the variable names were in English in all likelihood you can just estimate the missing component from either command from the model itself but I don’t even know what variables you are putting in since the description is not in English  
> **username_0**: I just fixed the variable names, what do you mean the df has errors? I ran the code and it did work.  
> **username_1**: this new example works I will have a closer look at your problem tonight or tomorrow evening  
> **username_0**: I really appreciate your help  

---

## 1 answers

### Answer by username_1 on 2020-08-14. Score: 3 | Accepted answer

Following the [prodtest](https://cran.r-project.org/web/packages/prodest/prodest.pdf) manual they define omega as residuals from the model, that is

$$\omega\_{it} = y\_{it} − (\alpha + w\_{it} \beta + k\_{it}\gamma)=y\_{it}-\hat{y}\_{it}$$

estprod does not have any comprehensible documentation but one way how we can get the above would be to extract the coefficients from the model and then calculate $y-\hat{y}$ manually.

We can do that with the broom package:

```
install.packages("broom")
require(broom) 
coefs<-tidy(mod1)
# A tibble: 3 x 3
  statistic   bias std.error
      <dbl>  <dbl>     <dbl>
1     -3.71   4.77      2.35
2     10.5  -11.8       4.26
3      1.73  -2.14      1.64

```

the names are off but the first column gives the same coefficients as when I inspect the model with summary so they are correct.

Next we can just can just calculate $y-\hat{y}$ as

```
omega_1 = (data$Y - data$Labor*coefs$statistic[1] - data$Capital*coefs$statistic[2] - data$Materials*coefs$statistic[3])  

```

the code is inelegant but it should do the job.

As a side note it might be worth while considering to estimate the TFP without these packages. Normally packages are the R's biggest strength but in this case estprod does not have any good accompanying documentation or manual and the prodest does not have the possibility to add the gross output yet as in the link the devs mentioned. In cases like this it might be better to just program your own function.

> **Comments:**
> **username_0**: Thank you very much for your contribution, it would be very good if you contribute to the elaboration of one of the packages. With regard to the development of the functions, I am still learning about TFP, so I have a little difficulty with the math behind it.  

---

