0

exams我对以下要与R 包一起使用的 Rmd 有疑问。它适用于html,但不适用于pdf,我不知道为什么。我在 html 中得到了带有 2 个面板的情节,但它只是在 pdf 中丢失了。谢谢!

```{r data generation, echo = FALSE, results = "hide"}
## DATA GENERATION
constr = sample(c("std","centered"),size=1)


n <- sample(35:65,1)

mx <- runif(1, 40, 60)
my <- runif(1, 200, 280)
sx <- runif(1, 9, 12)
sy <- runif(1, 44, 50)
a = runif(1,2,10)
b = sample(c(-3,1.2,3),size = 1)
e = rnorm(n)

r <- round(runif(1, 0.5, 0.9), 2)
x <- rnorm(n, mx, sd = sx)
y <- (r * x/sx + rnorm(n, my/sy - r * mx/sx, sqrt(1 - r^2))) * sy
mod1 = lm(y~x)
x1 = 0
y1 = 0

if (constr == "centered") {
   x1 = x - mx
   y1 = y - my
} else if (constr == "std") {
   x1 = (x - mx) / sx
   y1 = (y - my) / sy
}
mod2 = lm(y1~x1-1)


## QUESTION/ANSWER GENERATION
nq = 2
questions <- rep(list(""), nq)
solutions <- rep(list(""), nq)
explanations <- rep(list(""), nq)
type <- rep(list("string"),nq)

questions[[1]] = "What was the treatment we applied to the data? Or answer should either be `centered` or `standardized`. "
if (constr=="centered") {
    solutions[[1]] <- "centered"
    explanations[[1]] <- "We substracted the mean of each variable"
    questions[[2]] = "Give the value of the OLS slope coefficient in the right panel rounded to 3 digits"
    solutions[[2]] = round(coef(summary(mod1))[2],3)
    explanations[[2]] = "in a centered regression without an intercept, the slope coefficient is identical to the slope of the uncentered data (regression run with intercept)"
} else if (constr == "std") {
    solutions[[1]] <- "standardized"
    explanations[[1]] <- "You can see that both variables are centered on zero and have a standard deviation of around one."
    questions[[2]] = "Give the value of correlation coefficient $r$ between $x$ and $y$ in the left panel  rounded to 3 digits"
    solutions[[2]] = round(coef(summary(mod2))[1],3)
    explanations[[2]] = "in a standardized regression without an intercept, the slope coefficient is identical to correlation coefficient"
}
type[[2]] <- "num"
```

Question
========

We have a dataset with `r n` observations on $x$ and $y$. We apply some treatment to the data and transform it to $y1$ and $x1$. The left panel below is the data before, the right panel is the data after treatment:

```{r baplot,echo=FALSE,fig.align='center',fig.width=8}
par(mfrow=c(1,2))
plot(y~x,main="before")
plot(y1~x1,main="after")
if (constr=="centered"){
    abline(mod2)
}
```

and you are given the OLS estimates of a regression `r ifelse(constr=="centered","of y on x with","of y1 on x1 without")` an intercept for the data in the `r ifelse(constr=="centered","left","right")` panel:

```{r,echo=FALSE}
if (constr=="centered"){
    coef(mod1)
} else {
    coef(mod2)
}
```

```{r questionlist, echo = FALSE, results = "asis"}
answerlist(unlist(questions), markup = "markdown")
```

Solution
========

```{r solutionlist, echo = FALSE, results = "asis"}
answerlist(unlist(solutions),paste(unlist(explanations), ".", sep = ""), markup = "markdown")
```

Meta-information
================
extype: cloze
exsolution: `r paste(solutions, collapse = "|")`
exclozetype: `r paste(type, collapse = "|")`
exname: regconstrained
extol: 0.05
4

1 回答 1

0

TL;DR省略fig.align='center'

背景:如果没有,在 .Rmd 练习上fig.align运行会产生简单的 Markdown 输出:knitr::knit()

![plot of chunk baplot](figure/baplot-1.png)

但是,当包含它时,会knitr::knit()产生 HTML:

<img src="figure/baplot-1.png" title="plot of chunk baplot" alt="plot of chunk baplot" style="display: block; margin: auto;" />

这在将 Markdown 转换为 HTML 时具有预期的效果,pandoc因为它只是保留了 HTML。但是当pandoc通过 LaTeX 转换为 PDF 时,它不知道如何处理 Markdown 中的 HTML。

因此,非常模块化的设计在exams这里不起作用,因为这样的格式细节无法在标准 Markdown 中表示。有些 Markdown 方言具有此类扩展名,但并非所有 Markdown 处理器都支持这些方言。这就是为什么典型的建议是 Markdown 应该与所需的输出标记混合,通常是 HTML。

因此,对于 R/exams,我通常建议保持格式尽可能简单,以便练习在许多可能的输出格式中稳健地工作。居中在这里可能并不是真正重要的。

于 2018-11-24T13:05:25.840 回答