我正在尝试根据适合真实数据集的 logit 模型的结果开发考试。我尝试加载数据集,拟合模型,并使用r varname
语法包含从模型中提取的一些变量。
我首先使用练习中生成的人工数据开发了一个小示例。效果很好,这是相应的 Rmd 文件:
```{r data generation, echo = FALSE, results = "hide"}
library(tidyverse)
d <- tibble(y = rbinom(100, 1, 0.6), x1 = rnorm(100), x2=rnorm(100))
# randomize exams
nsize <- sample(50:150, 1)
sampled_dat <- sample(1:nrow(d), nsize, replace = TRUE)
fd <- d[sampled_dat, ]
fmodel <- glm(y ~ x1 + x2, data = fd, family = binomial("logit"))
```
Question
========
`r nrow(fd)`
```{r}
summary(fmodel)
```
Choose the correct answer.
Answerlist
----------
* sol1 `r nrow(fd)`
* sol2
Meta-information
================
exname: bdvDeviance
extype: schoice
exsolution: 10
exshuffle: TRUE
```
这在启动时按预期工作
elearn_exam <- c("ess3.Rmd")
set.seed(1234567)
exams2nops(elearn_exam, n = 2, language = "en",
institution = "U", title = "Exam",
dir = "nops_pdf", name = "BDV", date = "2018-01-08", duplex = FALSE)
然而,这是加载真实数据集的类似练习:
```{r data generation, echo = FALSE, results = "hide"}
load("d.Rdata")
# randomize exams
nsize <- sample(180:250, 1)
sampled_dat <- sample(1:nrow(d), nsize, replace = TRUE)
fd <- d[sampled_dat, ]
logitModel <- glm(Adopted ~ CultArea + Trained + LabRice+ Education + ExtContact, data = fd, family=binomial("logit"))
```
Question
========
`r nrow(fd)`
Choose the correct answer.
Answerlist
----------
* When adding variables, the deviance did not change. The variables did not bring some useful information.
* sol2 `r nrow(fd)`
Meta-information
================
exname: bdvDeviance
extype: schoice
exsolution: 10
exshuffle: TRUE
```
这一次,我收到以下错误:
> elearn_exam <- c("ess4.Rmd")
> set.seed(1234567)
> exams2nops(elearn_exam, n = 2, language = "en",
+ institution = "Uu", title = "Exam",
+ dir = "nops_pdf", name = "BDV_R", date = "2018-01-08", duplex = FALSE)
Quitting from lines 14-35 (ess4.Rmd)
Error in nrow(fd) : object 'fd' not found
我不明白第二种情况的问题是什么。显然,将fd
变量包含在r fd
. 问题不是来自回归,因为在编织 Rmd 文件时效果很好。