Search code examples
rr-exams

plot appears in html exam, but not in pdf version


I have a problem with the following Rmd to be used with the exams R package. it works for html, but not for pdf and i can't figure out why. I get the plot with 2 panels in html, but it's just missing in teh pdf. thanks!

```{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

Solution

  • TL;DR Omit fig.align='center'.

    Background: Without the fig.align, running knitr::knit() on the .Rmd exercise yields plain Markdown output:

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

    However, when including it, knitr::knit() yields HTML:

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

    This has the desired effect when pandoc converts the Markdown to HTML because it simply preserves the HTML. But when pandoc converts to PDF via LaTeX it does not know how to handle the HTML in the Markdown.

    Thus, the very modular design of exams does not work here because such formatting details cannot be represented in standard Markdown. There are Markdown dialects that have such extensions but these are not supported by all Markdown processors. That is why the typical recommendation is that Markdown should be mixed with the desired output markup, typically HTML.

    Therefore, for R/exams I typically recommend to keep the formatting as simple as possible so that the exercise works robustly in many possible output formats. And centering probably isn't really essential here.