Search code examples
rstatagenerate

Does syntax like tab() gen() exist in R?


I am now trying to convert stata to R and I have the following codes in stata:

tsset code_numeric year_numeric
sort code_numeric year_numeric
tab year, gen (yr)
tab code, gen(cd)
reg fhpolrigaug L.(fhpolrigaug lrgdpch) yr* cd* if sample==1, cluster(code)

Does anyone know which syntax should I use in R to get the same result as in stata? To be concrete, I mean the tab year, gen(yr), tab code, gen(cd) and reg fhpolrigaug L.(fhpolrigaug lrgdpch) yr* cd* if sample==1, cluster(code) parts. The dataset I used is in the link: https://www.openicpsr.org/openicpsr/project/113251/version/V1/view


Solution

  • I guess this is more or less what you want to achieve:

    setwd("your_directory/")
    library(plm)
    library(readxl)
    df <-read_excel("Income-and-Democracy-Data-AER-adjustment.xls", sheet = "10 Year Panel")
    
    df <- pdata.frame(df, index = c("code", "year"), drop.index = FALSE)
    
    #produce lagged variables if needed:
    #df$fhpolrigaug_lag <- lag(df$fhpolrigaug)
    #df$lrgdpch_lag <- lag(df$lrgdpch)
    
    
    regModel <- plm(fhpolrigaug ~ lag(fhpolrigaug) + lag(lrgdpch),
              data = subset(df, sample==1) , index = c("code","year"), model = "within", effect = "twoways")
    summary(regModel)
    
    #for more info, look here: https://philippbroniecki.com/statistics1/seminar10.html
    

    There is one thing left; you will have to find out on your own how to cluster the standard errors.

    PS: Here is a nice side-by-side comparison of R and Stata commands: http://rslblissett.com/wp-content/uploads/2016/09/sidebyside_130826.pdf