与运行块相比,编织无法正常工作

时间:2019-05-18 00:34:01

标签: r markdown r-markdown

编织降价文档时,以下块仅在s数据帧中生成NA,但独立运行块可按预期运行。 lmer()函数中需要的所有必需data.frames都是在markdown文档中创建的。我认为这可能与get()函数有关,但不了解为什么它可以与运行块一起工作,但在编织时却无法工作。

s=data.frame(row.names = c("ss_on","ss_off","nonss_on","nonss_off","log_draws","log_length","ss_on_co2"),
             var_names=c("ss_on_","ss_off_","nonss_on_","nonss_off_","log_draws_","log_length_","ss_on_co2_"),
             log_twh=c(T,T,F,T,F,T,F),
             log_swh=c(T,T,T,T,T,F,F),
             twh = "",twh_uci = "",twh_lci = "",swh="",swh_uci="",swh_lci="")

for(i in which(s$log_twh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=t$coefficients[1]
  s$twh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$twh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}
for(i in which(s$log_swh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=t$coefficients[1]
  s$swh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$swh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}

for(i in which(s$log_twh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=exp(t$coefficients[1])
  s$twh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$twh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}

for(i in which(s$log_swh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=exp(t$coefficients[1])
  s$swh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$swh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}
print(s)

插入到数据帧s中的所有值在编织时均为NA,但在运行块时正常运行。

1 个答案:

答案 0 :(得分:0)

您应该检查环境是否干净。运行您提供的代码会引发一些错误(请参见下文)。

这似乎是导致代码在控制台或块中按预期运行但在编织时失败的最常见原因。

library(lme4)
#> Loading required package: Matrix
s=data.frame(row.names = c("ss_on","ss_off","nonss_on","nonss_off","log_draws","log_length","ss_on_co2"),
             var_names=c("ss_on_","ss_off_","nonss_on_","nonss_off_","log_draws_","log_length_","ss_on_co2_"),
             log_twh=c(T,T,F,T,F,T,F),
             log_swh=c(T,T,T,T,T,F,F),
             twh = "",twh_uci = "",twh_lci = "",swh="",swh_uci="",swh_lci="")

for(i in which(s$log_twh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=t$coefficients[1]
  s$twh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$twh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'nonss_on_twh' not found
for(i in which(s$log_swh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=t$coefficients[1]
  s$swh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$swh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'log_length_swh' not found

for(i in which(s$log_twh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=exp(t$coefficients[1])
  s$twh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$twh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'ss_on_twh' not found

for(i in which(s$log_swh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=exp(t$coefficients[1])
  s$swh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$swh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'ss_on_swh' not found
print(s)
#>              var_names log_twh log_swh twh twh_uci twh_lci swh swh_uci
#> ss_on           ss_on_    TRUE    TRUE                                
#> ss_off         ss_off_    TRUE    TRUE                                
#> nonss_on     nonss_on_   FALSE    TRUE                                
#> nonss_off   nonss_off_    TRUE    TRUE                                
#> log_draws   log_draws_   FALSE    TRUE                                
#> log_length log_length_    TRUE   FALSE                                
#> ss_on_co2   ss_on_co2_   FALSE   FALSE                                
#>            swh_lci
#> ss_on             
#> ss_off            
#> nonss_on          
#> nonss_off         
#> log_draws         
#> log_length        
#> ss_on_co2

reprex package(v0.2.1)于2019-05-18创建