很抱歉,如果这个问题非常愚蠢。我正在尝试运行回归并将汇总表和诊断图发送到PDF文件。我发现这个软件包非常有用,但有两个基本问题。
感谢您的任何建议。
这是我的代码:
\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}
在块中包含循环时发现格式问题。根据Yihui的建议,我跑了
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
答案 0 :(得分:4)
你可以简化你的例子。两个问题的答案(有关详细文档,请参阅http://yihui.name/knitr/options):
results='asis'
; png
设备,可以为块设置较小的dpi
; R的pdf()
设备已经压缩了图表,如果默认的PDF压缩对您不满意,png
可能是唯一的选择;默认dpi
为72,我认为较小的值不会有很大帮助这是一个最小的例子:
\documentclass{article}
\begin{document}
hi
<<output-tex, results='asis'>>=
x = rnorm(1000)
cat('\\newpage')
@
here is a plot
<<smaller-png, dev='png', dpi=30>>=
plot(x)
@
\end{document}