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