2016-12-07 163 views
0

我一直在尋找一個等同於一個衆所周知的模式在Stata爲循環狀線性迴歸過程來重新創建塔塔的迴歸循環。下面是從Dehejia和沃赫拜一個例子(1999)最佳途徑中的R

local data "nswre74 cps1re74 cps3re74 " 
local datashort "cps1re74 cps3re74" 

local spec1 "" 
local spec2 "age age2 ed black hisp nodeg" 
local spec3 "re75" 
local spec4 "re75 age age2 ed black hisp nodeg" 
local spec5 "re74 re75 age age2 ed black hisp nodeg" 
local spec2a "age age2 ed black hisp nodeg married" 
local spec4a "re75 age age2 ed black hisp nodeg married" 
local spec5a "re74 re75 age age2 ed black hisp nodeg married" 

foreach set in `data' { 
    local lctr = 0 
    u `path'/`set', clear 
    forvalues t = 1/5 { 
     local ++lctr 
     reg re78 treat `spec`t'' 
      local b = _b[treat] 
      local se = _se[treat] 
      local b = round(`b',1) 
      local se = round(`se',1) 
      local line`lctr' "`line`lctr''& `b' & (`se') " 
     if `t' == 2 | `t' == 4 | `t' == 5 { 
      local ++lctr 
      reg re78 treat `spec`t'a' 
      local b = _b[treat] 
      local se = _se[treat] 
      local b = round(`b',1) 
      local se = round(`se',1) 
      local line`lctr' "`line`lctr''& `b' & (`se') " 
     } 
    } 
}' 

我並不需要在整個結構重新創建的,但想知道是否爲R模擬此功能:

reg re78 treat `spec`t'' 
使用for循環內

這裏,Stata的運行與編程可更換組間的線性迴歸共個變量「T」。我如何在R中複製這樣的東西?我試過這樣的:

spec1 <- "" 
spec2 <- "age+age2+ed+black+hisp+nodeg" 
spec3 <- "re75" 
spec4 <- "re75+age+age2+ed+black+hisp+nodeg" 
spec5 <-"re74+re75+age+age2+ed+black+hisp+nodeg" 
spec2a <- "age+age2+ed+black+hisp+nodeg+married" 
spec4a <- "re75+age+age2+ed+black+hisp+nodeg+married" 
spec5a <- "re74+re75+age+age2+ed+black+hisp+nodeg+married" 

for(i in 1:5){ 
fla <- as.formula(paste("y ~", as.formula(paste0("spec", as.character(i))), collapse="")) 
lm(fla, data = data) 
} 

但無濟於事顯而易見的原因。

+2

存儲每個規格爲列表中的一個特徵向量。然後用'reconfulate'通過列表'lapply'來構造公式。得到的迴歸模型將存儲在一個列表中,以便於組織訪問。如果需要,你可以的名字添加到列表中,然後用'paste0'調用在'for'環(保存結果到一個列表,當然),這將是更類似於塔塔法列表中的元素。 – lmo

回答

1
specs = list("1", 
       "age+age2+ed+black+hisp+nodeg", 
       "re75", 
       "re75+age+age2+ed+black+hisp+nodeg", 
       "re74+re75+age+age2+ed+black+hisp+nodeg", 
       "age+age2+ed+black+hisp+nodeg+married", 
       "re75+age+age2+ed+black+hisp+nodeg+married", 
       "re74+re75+age+age2+ed+black+hisp+nodeg+married" 
) 

for(each in specs){ 
    fla = as.formula(paste0("y ~", each)) 
    print(lm(fla, data = data)) 
}