对不起,如果这个问题很愚蠢。我正在尝试运行回归并将汇总表和诊断图发送到 PDF 文件。我发现这个包非常有用,但有两个基本问题。
- 我可以使用 LaTeX 格式化块内的页面吗?
- 有没有办法减小绘图的大小(我需要减小 pdf 文件的大小)?我尝试设置 dpi 并使用 png 而不是 plot,但没有成功。
感谢您的任何建议。
这是我的代码:
\documentclass[10pt]{article}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{setspace}
\usepackage[margin = 1in]{geometry}
\begin{document}
\SweaveOpts{concordance=TRUE}
\section{Risk $\sim$ log(Parameters)}
<<fig.width=5, fig.height=4.5, message=FALSE, tidy=TRUE, fig.align="center", echo=FALSE, warning=FALSE, eval=TRUE>>=
library(R.matlab)
for (k in 1:6){
data_t=readMat("I:\\Dropbox\\Rregression\\matlab_size.mat")
data=data.frame(NA, nrow = length(data_t[[1]]), ncol = 7)
data=data.frame(rho=data_t[[1]],Risk=data_t[[2]],Mass.f=data_t[[3]],Mass.utf=data_t[[4]],Mass.w=data_t[[5]],
ach=data_t[[6]],p=data_t[[7]])
data2=log(subset(data,select=-c(Risk)))
N_reg=dim(data2)[2]
num_ele=k
Comb=combn(N_reg,num_ele)
N_comb=ncol(Comb)
Results=data.frame(NA,ncol=4,nrow=N_comb)
TSS=sum((data$Risk-mean(data$Risk))^2)
for (i in 1:N_comb){
data_temp=data2[,Comb[,i]]
lm_temp=lm(data$Risk~as.matrix(data_temp))
name=paste("lm_", i, sep="")
assign(name, lm_temp)
Results[i,1]=summary(lm_temp)$r.squared
Results[i,2]=summary(lm_temp)$adj.r.squared
Results[i,3]=sum(resid(lm_temp)^2)
Results[i,4]=sum((fitted(lm_temp)-mean(fitted(lm_temp)))^2)
}
colnames(Results)=c("R2", "Adj_R2", "RSS", "ESS")
best_col=which(Results[,2]==max(Results[,2]))
cat ('Number of independent variables=', k)
print (summary(eval(parse(text=(paste("lm_", best_col, sep=""))))))
print (Results[best_col,])
par(mfrow=c(2,2),mar=c(3,3,3,3))
plot(eval(parse(text=(paste("lm_", best_col, sep=""))))) #question 2: is there a way to reduce dpi of plot?
\newpage #Here is question 1, I want to add a page break after printing a pair of summary table and plots
}
@
\end{document}
更新
当您在块中涉及循环时发现格式问题。根据易辉的建议,我跑了
library(devtools)
install_github('evaluate', 'hadley')
install_github('knitr', 'yihui')
在 R 控制台中。但是,遇到了与小插图相关的以下错误:
* installing the package to re-build vignettes
* creating vignettes ... ERROR
C:/PROGRA~1/R/R-215~1.1/bin/Rscript -e "library(knitr); knit2pdf('knitr-intro.Rnw')"
/usr/bin/sh: -c: line 0: syntax error near unexpected token `('
/usr/bin/sh: -c: line 0: `C:/PROGRA~1/R/R-215~1.1/bin/Rscript -e \library(knitr); knit2pdf(knitr-intro.Rnw)\'
make: *** [knitr-intro.pdf] Error 1
Error in tools::buildVignettes(dir = ".") : running 'make' failed
Execution halted
Error: Command failed (1)
In addition: Warning message:
running command '"C:/PROGRA~1/R/R-215~1.1/bin/x64/R" --vanilla CMD build "C:\Users\th\AppData\Local\Temp\RtmpSMRGAq\yihui-knitr-568a092" --no-manual --no-resave-data' had status 1
那么,关于如何解决这个问题的任何建议?我已经安装了 MikTex 2.9,并尝试直接从 Git 下载 Knitr 包并在 Windows shell 中构建。我有以下错误:
C:\Users\th\Documents\R\win-library\2.15>R CMD build knitr --no-vignettes
* checking for file 'knitr/DESCRIPTION' ... OK
* preparing 'knitr':
* checking DESCRIPTION meta-information ... OK
Warning in file(con, "r") :
cannot open file 'man': No such file or directory
Error in file(con, "r") : cannot open the connection
ERROR
computing Rd index failed