Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

r - What's wrong with this (gibbs) code?

I'm trying to run this code with R, but it gives me error:

rm(list = ls())
gibbs <- function (n, k, y0, m, alpha, beta){
  z <- array (0,n)
  for (i in 1 : n) {
    x <- array (0,k)
    y <- array (0, k+1)
    y[1] <- y0
    for (j in 1 : k) {
      x[j] <- rbinom (1, m, y[j])
      y[j+1] <- rbeta (1, x[j]+alpha, m-x[j]beta)
    }
    z[i]=x[k]
  }
  print (mean(z))
  print (var(z))
}

gibbs(1000, 50, 0.5, 3, 8.5, 4.5)

What exactly is wrong with it? here's the error it gives:

 source("E:\Program Files\R\test.R")
Error in source("E:\Program Files\R\test.R") : 
  E:Program FilesR	est.R:11:39: unexpected symbol
10: x[j] <- rbinom (1, m, y[j])
11: y[j+1] <- rbeta (1, x[j]+alpha, m-x[j]beta

Thanks in Advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

R does not know what operation you want between x[j] and beta in this expression m-x[j]beta. If you want to multiply, put a * there. If you want to add, put a + there.

It expected an operator there, so the b that starts beta is Unexpected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...