(可复制的代码)我正在研究Ugarte2016的“概率和统计与R”2E。以下代码在R中运行,但不处理类似Latex的代码。似乎"$...$"
内的代码未被处理。下面提供的代码来自本书的作者。某种程度上似乎有问题。可能是什么问题?
######### Chapter 12 #############
library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
################ Figure 12.1 ###############
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n", xlim = c(0, 12), ylim = c(-2, 12), xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10), labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10), labels = c("$E(Y|x_1) = \\beta_0 + \\beta_1x_1$", "$E(Y|x_1) = \\beta_0 + \\beta_1x_1$", "$E(Y|x_1) = \\beta_0 + \\beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \\beta_0 + \\beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
答案 0 :(得分:0)
sandipan的解决方案使用latex2exp::TeX
。有一个解决方案可以保留原始代码,而根本不使用latex2exp::TeX
。
当我联系本书的作者时,他们慷慨地发送了一个代码并指明他们使用tikzDevice
和knitr
来生成图表。作为knitr
/ tkizDevice
的新手,我找到了一种获取图像的方法,就像书中一样(斜体上的LateX&#39; ed chars);我相信必须有更好的方法:
tikzDeviceAndKnitr.Rnw
文件放在R的工作目录中(可以通过getwd()
找到)。
tikzDeviceAndKnitr.Rnw :
<<PASWR2fCh12S1, echo=FALSE, dev="tikz", crop=TRUE, fig.align='center', results='hide', fig.height=5, fig.width=7, out.width='0.95\\linewidth', warning=FALSE>>=
library(tikzDevice)
tikz('tikzDeviceAndKnitr.tex', standAlone=TRUE, width=5, height=5)
opar <- par(no.readonly = TRUE)
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n",
xlim = c(0, 12), ylim = c(-2, 12),
xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10),
labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10),
labels = c("$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \\beta_0 + \\beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
dev.off()
tools::texi2dvi('tikzDeviceAndKnitr.tex',pdf=T)
system(paste(getOption('pdfviewer'), 'tikzDeviceAndKnitr.pdf'))
@
在Windows的MikTeX中,安装与tikz
和pgf
相关的软件包。
将库加载到R和knit
相关的.Rnw文件中:
library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
library(latex2exp); library(knitr);library(tikzDevice);library(tools)
library(evaluate); library(markdown)
knit("tikzDeviceAndKnitr.Rnw") # The solution ended.
本书作者对我的回复是:
是的.... tikzDevice
与knitr
一起使用。完整的代码如下:
\begin{figure}[!ht]
<<c12slrModFIG, echo = FALSE, dev = "tikz", crop = TRUE, fig.align = 'center', results = 'hide', fig.height = 5, fig.width = 7, out.width='0.95\\linewidth', warning = FALSE>>=
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n",
xlim = c(0, 12), ylim = c(-2, 12),
xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10),
labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10),
labels = c("$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \\beta_0 + \\beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
@
\caption{Graphical representation of simple linear regression model
depicting the distribution of $Y$ given x \label{SLRgraph}}
\end{figure}