<p>Elaborating on the answer by @Jor Jorge Paredes, it is worth noting that the <code>idvar</code> needs to follow the original ID variable, while the above answer does not guarantee that this is the case and may create a wrong numbering.</p>
<p>A safer solution (edited):</p>
<pre><code>idlist &lt;- df[, c(&quot;time&quot;,&quot;original_id&quot;)] # id for every firm
idlist  &lt;- transform(idlist, idvar = as.numeric(factor(original_id)))
df &lt;- merge(df, idlist, by=c(&quot;original_id&quot;, &quot;time&quot;), all.x = T) #join it to the original dataset
</code></pre>
<p>EDIT: Or even smoother</p>
<pre><code> df &lt;- df %&gt;%
   mutate(idvar = as.numeric(factor(original_id)))
</code></pre>

CC BY-SA 4.0
2022-10-03T10:13:44.740Z
2022-10-18T09:14:35.203Z
2022-10-18T09:14:35.203Z
Bob
<p>Well, I figured out that the error meant something about not fitting in the dataset. My dataset had 78221 obs.</p>
<p>Everything was like <code>data(chilean)</code> the only difference had to be the id for each firm.</p>
<p>So I did this:</p>
<pre><code>list &lt;- data.frame(table(base<span class="math-container">$ruc)) # so here i got the string id for every firm
list$</span>idvar &lt;- seq(1:nrow(list)) # here i put a number for each firm from 1.
list &lt;- select(list, ruc, idvar) # We don't need freq var

base &lt;- base %&gt;% left_join(y=list, by=c(&quot;ruc&quot;=&quot;ruc&quot;)) #join it to the original dataset
</code></pre>
<p>And then it worked using <code>base$idvar</code> instead of using <code>base$ruc</code>.</p>

CC BY-SA 4.0
2022-10-03T10:03:53.563Z
thank you, this is really helping. I believe the issue was the IDs not being an ordered number starting from 1, which is really annoying
Bob
CC BY-SA 4.0
2021-08-10T02:29:34.897Z
2021-08-10T02:29:34.897Z
Jorge Paredes
<p>I have an unbalanced panel with 15063 firms between 2012 and 2018.</p>
<p>I am using this code to estimate the production function with Levinsohn &amp; Petrin method:</p>
<pre><code>levpet &lt;- prodestLP(Y=base$c_y,
 fX=base$c_l,
 sX=base$c_k,
 pX=base$c_m,
 idvar=base$ruc,
 timevar = base$year,
 R=100  )
</code></pre>
<p>where</p>
<ul>
<li><code>c_y</code> is log of value added</li>
<li><code>c_l</code> is log of wages</li>
<li><code>c_k</code> is log of capital</li>
<li><code>c_m</code> is log of materials</li>
<li><code>ruc</code> is the firm identifier (a string)</li>
<li><code>year</code> is a numeric.</li>
</ul>
<p>The error I am getting looks like this:</p>
<pre><code>Error in `[[&lt;-.data.frame`(`*tmp*`, i, value = c(42719L, 82109L, 82678L,  : 
  replacement has 469326 rows, data has 78221
</code></pre>
<p>I don´t know what it means or what I have to do about it.</p>
<p>How can I solve it? My data is very similar to this one:</p>
<pre><code>data(chilean)
</code></pre>
<p>data and <code>prodestLP</code>function are inside <code>library(prodest)</code></p>
<hr />
<p>These are all the libraries I have in the current script</p>
<pre><code>library(tidyverse)
library(dplyr)
library(foreign)
library(haven)
library(readxl)
library(stringr)
library(expss)
library(lubridate)
library(prodest)
library(plm)
</code></pre>
<p>Thanks in advance.</p>
<hr />
<p>Edit: With the dataset that's in the prodest package it runs just fine. Here's an example: <a href="https://rpubs.com/hacamvan/319728" rel="nofollow noreferrer">https://rpubs.com/hacamvan/319728</a></p>
<p>My dataset is very similar, just way more observations, and a string idvar.</p>
<hr />
<p>I think I've made it.</p>
<p>I had to assign a numerical id for every firm. then it worked.</p>
<p>now I just hope I can export the regressions.</p>

CC BY-SA 4.0
2021-08-08T16:16:58.793Z
Can you post your own solution as an answer and accept it and then question can be closed.
Jesper Hybel
<production-function><productivity>
Error while estimating a production function with prodest package in R
Jorge Paredes
# Error while estimating a production function with prodest package in R

(Asked by: username_0 on 2021-08-07)

I have an unbalanced panel with 15063 firms between 2012 and 2018.
I am using this code to estimate the production function with Levinsohn & Petrin method:
levpet <- prodestLP(Y=base$c_y,
 fX=base$c_l,
 sX=base$c_k,
 pX=base$c_m,
 idvar=base$ruc,
 timevar = base$year,
 R=100  )

where

c_y is log of value added
c_l is log of wages
c_k is log of capital
c_m is log of materials
ruc is the firm identifier (a string)
year is a numeric.

The error I am getting looks like this:
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(42719L, 82109L, 82678L,  : 
  replacement has 469326 rows, data has 78221

I don´t know what it means or what I have to do about it.
How can I solve it? My data is very similar to this one:
data(chilean)

data and prodestLPfunction are inside library(prodest)

These are all the libraries I have in the current script
library(tidyverse)
library(dplyr)
library(foreign)
library(haven)
library(readxl)
library(stringr)
library(expss)
library(lubridate)
library(prodest)
library(plm)

Thanks in advance.

Edit: With the dataset that's in the prodest package it runs just fine. Here's an example: https://rpubs.com/hacamvan/319728
My dataset is very similar, just way more observations, and a string idvar.

I think I've made it.
I had to assign a numerical id for every firm. then it worked.
now I just hope I can export the regressions.

**Tags:** production-function, productivity

> **Comments:**
> **username_1**: Can you post your own solution as an answer and accept it and then question can be closed.  

---

## 2 answers

### Answer by username_0 on 2021-08-10. Score: 3 | Accepted answer

Well, I figured out that the error meant something about not fitting in the dataset. My dataset had 78221 obs.
Everything was like data(chilean) the only difference had to be the id for each firm.
So I did this:
list <- data.frame(table(base$ruc)) # so here i got the string id for every firm
list$idvar <- seq(1:nrow(list)) # here i put a number for each firm from 1.
list <- select(list, ruc, idvar) # We don't need freq var

base <- base %>% left_join(y=list, by=c("ruc"="ruc")) #join it to the original dataset

And then it worked using base$idvar instead of using base$ruc.

> **Comments:**
> **username_2**: thank you, this is really helping. I believe the issue was the IDs not being an ordered number starting from 1, which is really annoying  

---

### Answer by username_2 on 2022-10-03. Score: 0

Elaborating on the answer by @Jor username_0, it is worth noting that the idvar needs to follow the original ID variable, while the above answer does not guarantee that this is the case and may create a wrong numbering.
A safer solution (edited):
idlist <- df[, c("time","original_id")] # id for every firm
idlist  <- transform(idlist, idvar = as.numeric(factor(original_id)))
df <- merge(df, idlist, by=c("original_id", "time"), all.x = T) #join it to the original dataset

EDIT: Or even smoother
 df <- df %>%
   mutate(idvar = as.numeric(factor(original_id)))


---

