<p>You are correct in noting that, adding <code>factor(FIPS)</code> in side the regression formula should be equivalent to specifiying <code>factor(FIPS) + YEARxQT</code> in the <code>felm()</code> fixed effects block. I am less certain that your <code>reg2</code> model that throws up the NA's is working correctly however, as that would typically throw up an error message before getting that result. Without actually seeing your data, I can only presume your issue is related to a bug, or has something very specific to do with your data.</p>
<p>Fortunately, there are lots of different ways to do this estimation, and it might be worth it to try this using different packages to corroborate the correct estimation and better understand what is going on with your specific use of <code>felm()</code>.</p>
<p>For example, just to check, I downloaded some sample panel data and ran a FE estimation using a binary for the main regressor. I run a Least Square Dummy Variable model, a FE using the <code>plm</code> package, and then two versions with <code>felm</code>, both placing the entity fixed effect inside and outside of <code>felm</code>'s FE block as you did in your example.  In all four cases the coefficient estimate was exactly the same (which it should be).</p>
<pre><code>library(foreign)
library(plm)
library(lfe)

Panel &lt;- read.dta(&quot;http://dss.princeton.edu/training/Panel101.dta&quot;) 

modlsdv &lt;-lm(y ~ op + factor(country) + factor(year), data=Panel)


modplm &lt;- plm(y ~ op + factor(year), data=Panel, index=c(&quot;country&quot;, &quot;year&quot;), model=&quot;within&quot;)


modfelm1 &lt;- felm(y ~ op | country + year | 0 | country, data=Panel)

#in this last model, I move the entity FE outside the FE block
modfelm2 &lt;- felm(y ~ op + factor(country) | year | 0 | country, data=Panel)
</code></pre>
<p>I only show the coefficient of interest as all the factored-out variables take up a lot of space, but you can see below that they are all equivalent (apart from the SE's in the <code>felm</code> models being different because of how they were clustered).</p>
<pre><code>────────────────────────────────────────────────────────────────────────────
                     LSDV           plm           felm1          felm2      
               ─────────────────────────────────────────────────────────────
  (Intercept)    419655690.33                                               
                          3                                                 
                 (1320579554.                                               
                        769)                                                
  op             -1434277502.   -1434277502.   -1434277502.   -1434277502.  
                        244            244              244            244  
                                                          *            *    
                 (771947977.3   (771947977.3   (524652684.0   (594276313.4  
                         01)            01)            46)          03) 
               ─────────────────────────────────────────────────────────────
  N                  70             70             70           70          
  R2                  0.386          0.264          0.386        0.386      
  logLik          -1609.649                     -1609.649     -1609.649     
                                                                            
  AIC              3255.298                      3255.298     3255.298      
──────────────────────────────────────────────────────────────────────────
</code></pre>

CC BY-SA 4.0
2020-11-25T06:26:45.877Z
2020-11-25T06:26:45.877Z
anguyen1210
<p>I am wondering why using felm's fixed effect option versus using the factor() function is different? I was under the impression they were one in the same?</p>
<p>I am running a panel regression where i is the a unique FIPS code and t is every quarter. The variable of interest, a binary of whether the FIPS flooded or not, is constant across time. However, my y, housing prices, varies each with each t. When I use felm()'s fixed effect option for FIPS, flooded is NA. However, using factor(FIPS), gives an estimate for the variable of interest.</p>
<p>I'm under the impression felm()'s fixed effect function is working correctly. Since being flooded is time constant and has no variation within a given FIPS, the fixed effect is absorbing the effect of flooding. However, I'm not sure why factor(FIPS) within the regression would return an estimate since a fixed effect essentially the same thing?</p>

CC BY-SA 4.0
2020-11-24T09:13:56.687Z
This is maybe a better question for StackOverflow, but out of curiousity, what are the two different commands you are entering for these different results?
anguyen1210
CC BY-SA 4.0
2020-11-24T13:52:31.217Z
Yeah I was torn between where to post but I figured this forum would be more familiar with econometric theory. The one that returns an estimate: `reg1 <- felm(HPI~FLOODED+ lag(dat.reg[,'HPI'],1) + factor(FIPS) | YEARxQT | 0 | FIPS, data= dat.reg)`. The one that returned NA: `reg2 <- felm(HPI~FLOODED+ lag(dat.reg[,'HPI'],1) | FIPS+YEARxQT | 0 | FIPS, data= dat.reg)`.
DPek
CC BY-SA 4.0
2020-11-25T06:06:44.363Z
Did your `reg2` call give you a Warning message?
anguyen1210
<regression><paneldata><r><fixed-effects>
R's felm() fixed effects vs. factor() within lm()
DPek
# R's felm() fixed effects vs. factor() within lm()

(Asked by: username_0 on 2020-11-23)

I am wondering why using felm's fixed effect option versus using the factor() function is different? I was under the impression they were one in the same?

I am running a panel regression where i is the a unique FIPS code and t is every quarter. The variable of interest, a binary of whether the FIPS flooded or not, is constant across time. However, my y, housing prices, varies each with each t. When I use felm()'s fixed effect option for FIPS, flooded is NA. However, using factor(FIPS), gives an estimate for the variable of interest.

I'm under the impression felm()'s fixed effect function is working correctly. Since being flooded is time constant and has no variation within a given FIPS, the fixed effect is absorbing the effect of flooding. However, I'm not sure why factor(FIPS) within the regression would return an estimate since a fixed effect essentially the same thing?

**Tags:** regression, paneldata, r, fixed-effects
> **Comments:**
> **username_1**: This is maybe a better question for StackOverflow, but out of curiousity, what are the two different commands you are entering for these different results?  
> **username_0**: Yeah I was torn between where to post but I figured this forum would be more familiar with econometric theory. The one that returns an estimate: `reg1 <- felm(HPI~FLOODED+ lag(dat.reg[,'HPI'],1) + factor(FIPS) | YEARxQT | 0 | FIPS, data= dat.reg)`. The one that returned NA: `reg2 <- felm(HPI~FLOODED+ lag(dat.reg[,'HPI'],1) | FIPS+YEARxQT | 0 | FIPS, data= dat.reg)`.  
> **username_1**: Did your `reg2` call give you a Warning message?  

---

## 1 answers

### Answer by username_1 on 2020-11-25. Score: 2

You are correct in noting that, adding factor(FIPS) in side the regression formula should be equivalent to specifiying factor(FIPS) + YEARxQT in the felm() fixed effects block. I am less certain that your reg2 model that throws up the NA's is working correctly however, as that would typically throw up an error message before getting that result. Without actually seeing your data, I can only presume your issue is related to a bug, or has something very specific to do with your data.
Fortunately, there are lots of different ways to do this estimation, and it might be worth it to try this using different packages to corroborate the correct estimation and better understand what is going on with your specific use of felm().
For example, just to check, I downloaded some sample panel data and ran a FE estimation using a binary for the main regressor. I run a Least Square Dummy Variable model, a FE using the plm package, and then two versions with felm, both placing the entity fixed effect inside and outside of felm's FE block as you did in your example.  In all four cases the coefficient estimate was exactly the same (which it should be).
library(foreign)
library(plm)
library(lfe)

Panel <- read.dta("http://dss.princeton.edu/training/Panel101.dta") 

modlsdv <-lm(y ~ op + factor(country) + factor(year), data=Panel)


modplm <- plm(y ~ op + factor(year), data=Panel, index=c("country", "year"), model="within")


modfelm1 <- felm(y ~ op | country + year | 0 | country, data=Panel)

#in this last model, I move the entity FE outside the FE block
modfelm2 <- felm(y ~ op + factor(country) | year | 0 | country, data=Panel)

I only show the coefficient of interest as all the factored-out variables take up a lot of space, but you can see below that they are all equivalent (apart from the SE's in the felm models being different because of how they were clustered).
────────────────────────────────────────────────────────────────────────────
                     LSDV           plm           felm1          felm2      
               ─────────────────────────────────────────────────────────────
  (Intercept)    419655690.33                                               
                          3                                                 
                 (1320579554.                                               
                        769)                                                
  op             -1434277502.   -1434277502.   -1434277502.   -1434277502.  
                        244            244              244            244  
                                                          *            *    
                 (771947977.3   (771947977.3   (524652684.0   (594276313.4  
                         01)            01)            46)          03) 
               ─────────────────────────────────────────────────────────────
  N                  70             70             70           70          
  R2                  0.386          0.264          0.386        0.386      
  logLik          -1609.649                     -1609.649     -1609.649     
                                                                            
  AIC              3255.298                      3255.298     3255.298      
──────────────────────────────────────────────────────────────────────────


---

