我为一堆乱七八糟的代码道歉,但我对R相对较新,当我输入以下代码时:
for(t in 2:M)
{
alphcandidate=mvrnorm(1,Gibbsalph[,t-1],as.numeric(csquared*4.93)*Zyesinv)
r=exp((-1/2)*(t(rep(1,105))%*%Z%*%(alphacandidate-Gibbsalph[,t-1])+sum((lnyield-X%*%Gibbsbeta[,t-1])^2)%*%(exp(-Z%*%alphcandidate)-exp(-Z%*%Gibbsalph[,t-1]))+t(alphcandidate-rep(0,m))%*%(solve(as.numeric(100)*diag(m)))%*%(alphcandidate-rep(0,m))-t(Gibbsalph[,t-1]-rep(0,m))%*%(solve(as.numeric(100)*diag(m)))%*%(Gibbsalph[,t-1]-rep(0,m)))
if(runif(1)<min(1,r)) {
Gibbsalph[,t]=alphcandidate
} else{
Gibbsalph[,t]=Gibbsalph[,t-1]
}
Dbetanawinv=t(X)%*%(diag(as.vector(1/(exp(Z%*%Gibbsalph[,t])))))%*%X+solve(as.numeric(100)*diag(n))
Dbetach=chol(Dbetanawinv)
Dbetachinv=solve(Dbetach)
Dbeta=Dbetachinv%*%t(Dbetachinv)
dbeta=t(X)%*%(diag(as.vector(1/(exp(Z%*%Gibbsalph[,t])))))%*%lnyield+(solve(as.numeric(100)*diag(n))%*%(rep(0,n)))
Gibbsbeta[,t]=mvrnorm(1,Dbeta%*%dbeta,Dbeta)
}
我收到以下错误: 错误:意外'如果'在: “如果”
错误:“}”中的意外“}”
错误:“}”中的意外“}”
我不确定为什么这个if语句不能正常工作。抱歉,我还不知道如何在本网站上正确格式化我的问题。任何帮助将不胜感激!
答案 0 :(得分:3)
您在)
行的末尾(?)错过r = exp(...
粘贴到RStudio中会显示缩进级别:
r=exp((-1/2)*(t(rep(1,105))%*%Z%*%(alphacandidate-Gibbsalph[,t-1])+sum((lnyield-X%*%Gibbsbeta[,t-1])^2)%*%(exp(-Z%*%alphcandidate)-exp(-Z%*%Gibbsalph[,t-1]))+t(alphcandidate-rep(0,m))%*%(solve(as.numeric(100)*diag(m)))%*%(alphcandidate-rep(0,m))-t(Gibbsalph[,t-1]-rep(0,m))%*%(solve(as.numeric(100)*diag(m)))%*%(Gibbsalph[,t-1]-rep(0,m)))
if(runif(1)<min(1,r)) {
Gibbsalph[,t]=alphcandidate