r - 与运行块相比,Knit 无法正常工作
问题描述
在编织 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,但在运行块时可以正常工作。
解决方案
你应该检查你的环境是否干净。运行您提供的代码会引发几个错误(见下文)。
这似乎是代码在控制台中按预期运行或逐块运行但在编织时失败的最常见原因。
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 包(v0.2.1)于 2019 年 5 月 18 日创建
推荐阅读
- java - 如何在 Android 中模糊触摸区域
- groovy - 如何在 TestHttpClient 中设置 POST 参数
- docusignapi - 尝试获取信封状态或文档时的 USER_DOES_NOT_BELONG_TO_SPECIFIED_ACCOUNT
- c++ - 如果系统磁盘在 Windows 2016 中是动态的,则系统保留分区和卷“C:\”具有相同的唯一 ID
- asp.net - 使用 Ajax 请求在 MVC 中上传 Excel 文件
- c# - datagrid wpf的特定动态列的上下文菜单
- excel - With函数中的多个对象表达式遍历所有工作表
- linux - 用其他字符串替换多个给定字符串
- python - bash 从一列中聚类数据并从另一列中提取其值
- java - 子级更新时未在父级中更新乐观锁定版本