运行/ usr / bin / pdflatex时出错(退出代码70)

时间:2014-11-08 22:17:16

标签: r

我的吉布斯采样代码是这样的:

我无法编译它。错误消息是Error running / usr / bin / pdflatex(退出代码70)。

我的代码中没有看到任何字体问题。任何人都可以帮我修复它吗?谢谢!

为什么?

\documentclass{article}
\usepackage{float}
\begin{document}
\SweaveOpts{concordance=TRUE}
\title{Lab 8 for STA 601}
\author{Siyang Li}
\date{2014/11/4}
\maketitle

\section{part 1}
see attached file

\section{part 2}
<<gibbssampling, echo = TRUE, fig = TRUE, include = TRUE>>=
library(msm)
library(coda)

## read in data, data is on a log scale
data <- read.table("data.txt", header = TRUE)
data1 <- data[data$day == "Saturday"|data$day == "Sunday",] ##data for weekday
data2 <- data[data$day == "Monday"|data$day == "Tuesday"|data$day == "Wednesday"|data$day == "Thursday"|data$day == "Friday",] ##data for weekend

dev.off()

S <- 20000

## to save the results
mu1.draws <- matrix(NA, nrow = S, ncol = 1)
mu2.draws <- matrix(NA, nrow = S, ncol = 1)
phi1.draws <- matrix(NA, nrow = S, ncol = 1)
phi2.draws <- matrix(NA, nrow = S, ncol = 1)

## starting points
mu1.draws[1,1] <- mu1.cur <- 1
mu2.draws[1,1] <- mu2.cur <- 1
phi1.draws[1,1] <- phi1.cur <- 0.01
phi2.draws[1,1] <- phi2.cur <- 0.1

##prior specification
m1 <- 0
v1 <- 0.01
m2 <- 0
v2 <- 0.02
alpha1 <- 0.1
beta1 <- 0.1
alpha2 <- 0.2
beta2 <- 0.2

n1 <- length(data1)
n2 <- length(data2)

##gibbs sampling

for(i in 2:S){
  ##update mu1
  sigma1.post <- 1/(n1*phi1.cur + 1/v1)
  mu1.post <- (sum(data1$pm)*phi1.cur + m1/v1)*sigma1.post
  mu1.cur <- rtnorm(1,mu1.post, sqrt(sigma1.post), mu2.cur,Inf)

  ##update phi1
  alpha1.post <- alpha1 + n1/2
  beta1.post <- beta1 + 1/2*sum((data1$pm - mu1.cur)^2)
  phi1.cur <- rgamma(1,alpha1.post, beta1.post)

  ##update mu2
  sigma2.post <- 1/(n2*phi2.cur + 1/v2)
  mu2.post <- (sum(data2$pm)*phi2.cur + m2/v2)*sigma2.post
  mu2.cur <- rtnorm(1,mu2.post,sqrt(sigma2.post),-Inf,mu1.cur)

  ##update phi2
  alpha2.post <- alpha2 + n2/2
  beta2.post <- beta2 +1/2*sum((data2$pm - mu2.cur)^2)
  phi2.cur <- rgamma(1,alpha2.post, beta2.post)

  ##save the results
  mu1.draws[i,1] <- mu1.cur
  mu2.draws[i,1] <- mu2.cur
  phi1.draws[i,1] <- phi1.cur
  phi2.draws[i,1] <- phi2.cur

}

Burnin <- 5000

##convergence diagonostics
mu.mcmc <- mcmc(mu1.draws)
plot(mu.mcmc)
autocorr.plot(mu.mcmc)

mean(mu1.draws[(Burnin+1):S,1])
mean(mu2.draws[(Burnin+1):S,1])
plot(mu1.draws[(Burnin+1):S,1])
plot(phi1.draws[(Burnin+1):S,1])
@


\section{part 3}
<<intervals,echo = TRUE, fig = TRUE, include = TRUE>>=
cat("mean value of mu1 is",mean(mu1.draws[(Burnin+1):S]),"\n")
cat("95% interval of mu1 is",quantile(mu1.draws[(Burnin+1):S],c(0.025,0.975)),"\n")
cat("mean value of mu2 is",mean(mu2.draws[(Burnin+1):S]),"\n")
cat("95% interval of mu2 is",quantile(mu2.draws[(Burnin+1):S],c(0.025,0.975)),"\n")
cat("mean value of phi1 is",mean(phi1.draws[(Burnin+1):S]),"\n")
cat("95% interval of phi1 is",quantile(phi1.draws[(Burnin+1):S],c(0.025,0.975)),"\n")
cat("mean value of phi2 is",mean(phi2.draws[(Burnin+1):S]),"\n")
cat("95% interval of phi2 is",quantile(phi2.draws[(Burnin+1):S],c(0.025,0.975)),"\n")
@

\section{part 4}
<<posteriorprob, echo = TRUE, fig = TRUE, include = TRUE>>=
mean(mu1.draws[(Burnin+1):S] > mu2.draws[(Burnin+1):S])
mean(phi1.draws[(Burnin+1):S] < phi2.draws[(Burnin+1):S])
@

\section{part 5}
<<prediction, echo = TRUE, fig = TRUE, include = TRUE>>=
for(i in (Burnin+1):S){
    mu1 <- mu1.draws[i]
    phi1 <- phi1.draws[i]
    mu2 <- mu2.draws[i]
    phi2 <- phi2.draws[i]

    y1 <- rnorm(1,mu1,sqrt(1/phi1))
    y2 <- rnorm(1,mu2,sqrt(1/phi2)) 
}

mean(y1 > y2)
@

\end{document}

0 个答案:

没有答案