Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r2 function when only fixed effects are used #27

Closed
luispfonseca opened this issue Jul 6, 2020 · 1 comment
Closed

r2 function when only fixed effects are used #27

luispfonseca opened this issue Jul 6, 2020 · 1 comment

Comments

@luispfonseca
Copy link

@luispfonseca luispfonseca commented Jul 6, 2020

Minor issue.

The r2 function returns an error when applied to a regression that only has fixed effects but no other covariates. I haven't checked this for all the objects returned, but adjusted r-squared is returned in the summary function in that case, so I assume it could also be returned when the r2 function is called.

library(fixest)
library(magrittr)

# create data
df <- data.frame(x = runif(1000))
df$id <- sample(20, 1000, TRUE)
df$y <- runif(1000) + 0.2 * df$x + 0.05 * df$id

# show that r2 works well in the normal case
fixest::feols(y ~ x | id, data = df) %>% 
  summary()
#> OLS estimation, Dep. Var.: y
#> Observations: 1,000 
#> Fixed-effects: id: 20
#> Standard-errors: Clustered (id) 
#>   Estimate Std. Error t value  Pr(>|t|)    
#> x 0.204396   0.020233  10.102 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> Log-likelihood: -151.67   Adj. R2: 0.485 
#>                         R2-Within: 0.04161

fixest::feols(y ~ x | id, data = df) %>% 
  fixest::r2()
#>     sq.cor         r2        ar2        pr2       apr2        wr2       war2 
#> 0.49531459 0.49531459 0.48500437 0.69270986 0.65218985 0.04161231 0.04063337 
#>       wpr2      wapr2 
#> 0.12289423 0.11711136

# show that there is an adjusted r-squared measure even for cases where only fixed-effects are used
fixest::feols(y ~ 1 | id, data = df) %>% 
  summary()
#> OLS estimation, Dep. Var.: y
#> Observations: 1,000 
#> Fixed-effects: id: 20
#> Log-likelihood: -172.92   Adj. R2: 0.46319

# error when calling r2 on this estimation
fixest::feols(y ~ 1 | id, data = df) %>% 
  fixest::r2()
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 0, 1000

Created on 2020-07-06 by the reprex package (v0.3.0)

Thank you for the great work in the package.

@lrberge
Copy link
Owner

@lrberge lrberge commented Jul 7, 2020

Thanks for pointing to this issue, now corrected! (The dev. version is not stable though)

@lrberge lrberge closed this Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.