我正在用 RMarkdown(在 RStudio 中使用 knitr)编写练习,其中包括一道数字题和一道单选题。我该如何编写正确的代码,让该活动显示在 Moodle 中?
数字问题需要输入相关系数(在下面的代码中其正确答案是“corr1”),而单选题需要选择代表数据的图表(其中“grafico1”是正确答案)。
library(ggplot2)
Concentration = c(7.1,82.9)
r.sq = 0
while (r.sq <0.85) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data$Reaction_time~data$Concentration)
summa = summary(z)
r.sq = summa$r.squared
}
corr = round(cor(data$Concentration,data$Reaction_time),2)
corr1 = corr
r.sq = 0
while (r.sq <0.85 | abs(corr1)<abs(corr)) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data1 = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data1$Reaction_time~data1$Concentration)
corr1 = round(cor(data1$Concentration,data1$Reaction_time),2)
summa = summary(z)
r.sq = summa$r.squared
}
corr2 = corr
r.sq = 0
while (r.sq <0.85 | abs(corr2)<abs(corr)) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data2 = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data2$Reaction_time~data2$Concentration)
corr2 = round(cor(data2$Concentration,data2$Reaction_time),2)
summa = summary(z)
r.sq = summa$r.squared
}
corr3 = corr
r.sq = 0
while (r.sq <0.85 | abs(corr3)<abs(corr)) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data3 = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data3$Reaction_time~data3$Concentration)
corr3 = round(cor(data3$Concentration,data3$Reaction_time),2)
summa = summary(z)
r.sq = summa$r.squared
}
write.csv(data,'concentration_vs_rxtime_q2.csv', row.names = FALSE)
p1 = ggplot(data, aes(x=Concentration, y=Reaction_time)) + geom_point()
p2 = ggplot(data1, aes(x=Concentration, y=Reaction_time)) + geom_point()
p3 = ggplot(data2, aes(x=Concentration, y=Reaction_time)) + geom_point()
p4 = ggplot(data3, aes(x=Concentration, y=Reaction_time)) + geom_point()
png('grafico1.png', width=380, height=248, units='px', pointsize=12)
p1
dev.off()
png('grafico2.png', width=380, height=248, units='px', pointsize=12)
p2
dev.off()
png('grafico3.png', width=380, height=248, units='px', pointsize=12)
p3
dev.off()
png('grafico4.png', width=380, height=248, units='px', pointsize=12)
p4
dev.off()
该代码在 Moodle 中不起作用,因为图表选项没有可用于选择正确答案的复选框。
Determine the correlation coefficinet:
##ANSWER1##
Choose the right plot for data:
1. ![](grafico1.png)
2. ![](grafico2.png)
3. ![](grafico3.png)
4. ![](grafico4.png)
Meta-information
================
extype: cloze
exclozetype: num|schoice
exsolution: `r corr1`|1000
exname: mod10_q1