Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...