我通常在Stata工作,所以我是R的新手。我试着理解如何运行循环,但即使阅读了不少教程,我也会收到错误。
我尝试运行:
foo = seq(0, 15000, by = 1000)
for (i in foo){
dbuf <- ei(formula=formula,total="n",data=FILENAME_[i], erho=0.5, id="firm_unique")
}
我想为FILENAME_0,FILENAME_1000,FILENAME_2000等运行ei功能......
但是,我得到了:
Error in ei.estimate(t, x, n, id = id, data = data, Zb = Zb, Zw = Zw, :
object 'SA3EU_EI_' not found
所以循环变量我没有正确地将其中一个数组值插入到循环中...知道为什么会发生这种情况吗?
非常感谢!
编辑附加信息:
FILENAME是作为数据框导入的CSV文件
EI是一个统计程序:http://gking.harvard.edu/eiR
> dput(formula)
dsntpct ~ freeflt
> dput(ei)
function (formula, total = NULL, Zb = 1, Zw = 1, id = NA, data = NA,
erho = 0.5, esigma = 0.5, ebeta = 0.5, ealphab = NA, ealphaw = NA,
truth = NA, simulate = TRUE, covariate = NULL, lambda1 = 4,
lambda2 = 2, covariate.prior.list = NULL, tune.list = NULL,
start.list = NULL, sample = 1000, thin = 1, burnin = 1000,
verbose = 0, ret.beta = "r", ret.mcmc = TRUE, usrfun = NULL)
{
dv <- terms.formula(formula)[[2]]
iv <- terms.formula(formula)[[3]]
t <- as.character(dv)
x <- as.character(iv)
n <- as.character(total)
id <- as.character(id)
if (length(dv) == 1) {
print("Running 2x2 ei")
if (simulate == FALSE) {
dbuf <- ei.estimate(t, x, n, id = id, data = data,
Zb = Zb, Zw = Zw, erho = erho, esigma = esigma,
ebeta = ebeta, ealphab = ealphab, ealphaw = ealphaw,
truth = truth)
return(dbuf)
}
if (simulate == TRUE) {
dbuf <- tryCatch(tryCatch(ei.estimate(t, x, n, id = id,
data = data, Zb = Zb, Zw = Zw, erho = erho, esigma = esigma,
ebeta = ebeta, ealphab = ealphab, ealphaw = ealphaw,
truth = truth), error = function(x) ei(t, x,
n, id = id, data = data, Zb = Zb, Zw = Zw, erho = 3,
esigma = esigma, ebeta = ebeta, ealphab = ealphab,
ealphaw = ealphaw, truth = truth)), error = function(x) ei.estimate(t,
x, n, id = id, data = data, Zb = Zb, Zw = Zw,
erho = 5, esigma = esigma, ebeta = ebeta, ealphab = ealphab,
ealphaw = ealphaw, truth = truth))
dbuf.sim <- ei.sim(dbuf)
return(dbuf.sim)
}
}
if (length(dv) > 1) {
print("Running eiRxC")
dbuf <- ei.MD.bayes(formula, data = data, total = total,
covariate = covariate, lambda1 = lambda1, lambda2 = lambda2,
covariate.prior.list = covariate.prior.list, tune.list = tune.list,
start.list = start.list, sample = sample, thin = thin,
burnin = burnin, verbose = verbose, ret.beta = ret.beta,
ret.mcmc = ret.mcmc, usrfun = usrfun)
dbuf$data <- data
dbuf$total <- n
dbuf$formula <- formula
class(dbuf) <- "ei"
return(dbuf)
}
}
>
答案 0 :(得分:0)
这可能有效:
foo = seq(0, 15000, by = 1000)
dbuf= list()
for (i in foo){
dbuf[i] <- ei(formula=formula, total="n", data=get(paste0(FILENAME_", i) ),
erho=0.5, id="firm_unique")
}
您忘记了两件至关重要的事情:1)需要初始化您要使用索引分配的列表,以及2)索引分配。否则,您只需从ei()...)
- 调用中获取最后一组结果。 get
函数返回一个命名对象,其中参数是一个字符值。