title: Clustered standard errors with texreg?
body: <p>I'm trying to reproduce <a href="http://www.ats.ucla.edu/stat/stata/webbooks/reg/chapter4/statareg4.htm" rel="nofollow">this</a> stata example and move from <code>stargazer</code> to <code>texreg</code>. The data is available <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=3&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwjmuJiLg-bLAhWK8CYKHfSGCakQFggsMAI&amp;url=https%3A%2F%2Fdl.dropboxusercontent.com%2Fu%2F28526762%2FAPS%2Felemapi2.dta&amp;usg=AFQjCNHo2xf6nVn6iJAwilMstoOCPj7C6Q&amp;sig2=njn9r3WlM20orIA7oywnug" rel="nofollow">here</a>.</p>
  
  <p>To run the regression and get the se I run this code:</p>
  
  <pre><code>library(readstata13)
  library(sandwich)
  cluster_se &lt;- function(model_result, data, cluster){
    model_variables   &lt;- intersect(colnames(data), c(colnames(model_result$model), cluster))
    model_rows &lt;- as.integer(rownames(model_result$model))
    data &lt;- data[model_rows, model_variables]
  
    cl &lt;- data[[cluster]]
    M &lt;- length(unique(cl))
    N &lt;- nrow(data)
    K &lt;- model_result$rank
    dfc &lt;- (M/(M-1))*((N-1)/(N-K))
    uj  &lt;- apply(estfun(model_result), 2, function(x) tapply(x, cl, sum));
    vcovCL &lt;- dfc*sandwich(model_result, meat=crossprod(uj)/N)
    sqrt(diag(vcovCL))
  }
  elemapi2 &lt;- read.dta13(file = 'elemapi2.dta')
  lm1 &lt;- lm(formula = api00 ~ acs_k3 + acs_46 + full + enroll, data = elemapi2)
  se.lm1 &lt;- cluster_se(model_result = lm1, data = elemapi2, cluster = "dnum")
  
  stargazer::stargazer(lm1, type = "text", style = "aer", se = list(se.lm1))
  
  ==========================================================
    api00                 
  ----------------------------------------------------------
    acs_k3                              6.954                 
                                        (6.901)                
  
  acs_46                             5.966**                
                                     (2.531)                
  
  full                               4.668***               
                                     (0.703)                
  
  enroll                             -0.106**               
                                     (0.043)                
  
  Constant                            -5.200                
                                      (121.786)               
  
  Observations                         395                  
  R2                                  0.385                 
  Adjusted R2                         0.379                 
  Residual Std. Error           112.198 (df = 390)          
  F Statistic                61.006*** (df = 4; 390)        
  ----------------------------------------------------------
    Notes:              ***Significant at the 1 percent level.
  **Significant at the 5 percent level. 
  *Significant at the 10 percent level. 
  </code></pre>
  
  <p><code>texreg</code> produces this:</p>
  
  <pre><code>texreg::screenreg(lm1, override.se=list(se.lm1))    
  
  ========================
    Model 1    
  ------------------------
    (Intercept)    -5.20    
                   (121.79)   
  acs_k3          6.95    
                  (6.90)   
  acs_46          5.97 ***
                  (2.53)   
  full            4.67 ***
                  (0.70)   
  enroll         -0.11 ***
                  (0.04)   
  ------------------------
    R^2             0.38    
  Adj. R^2        0.38    
  Num. obs.     395       
  RMSE          112.20    
  ========================  
  </code></pre>
  
  <p>How can I fix the p-values?</p>