-3

I'm wondering what I should be doing here (please refer to image). I have already defined two vectors which are k=c(0,1) and v=c(runif(2,0.3,0.7)) where alpha=v[1] and beta=v[2]. Afterwards, I used an if statement, if(Xn==k[1]){...} However this is where I am stuck at. According to the question, I have to assign Xn+1=k[1] with probability (alpha) at the same time Xn+1=k[2] with probability (1-alpha) and if(Xn==k[2]){...} then Xn+1=k[1] has probability (beta) and Xn+1=k[2] will have probability (1-beta).

So my question is how do you assign the values to the respective Xn+1 values of 0 and 1 with probabilities [(alpha), (1-alpha)] and [(beta),(1-beta)]. After assigning it, how do you then run a simulation of 500 observations from X1 to X500 of the random variable by using a for loop This is similar to the coin toss experiment with the exception being that probability of Heads and Tails are decided by [alpha,beta] = runif(2,0.3,0.7)`.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 4
    I cleaned up your formatting, but it's not **clear** what you've tried. You start by explaining that you defined `k` and `v`, but the question does not mention variables `k` or `v`. Can you explain them? Your inline code (and maybe your actual code) seems very sloppy on whether or not you use brackets for indexing. `Xn` and `Xn+1` has a very different meaning than `X[n]` and `X[n + 1]` and `X[n] + 1`. Please consistently use valid code syntax for what you are trying to do. – Gregor Thomas Oct 27 '21 at 19:17
  • 4
    Your attempt so far would be much clearer if you put the code in a single, **runnable** code block. Currently, if I try to run the code you share I can define `k` and `v`, but your line `if(Xn==k[1]){....} ` will be an error because `Xn` isn't defined. So please make your example reproducible by sharing all the definitions, explaining what your variables mean, and explaining your attempt. – Gregor Thomas Oct 27 '21 at 19:19
  • Alright thanks. Sorry I did not notice your comment. Will do better onwards – Jose Moquiambo Nov 01 '21 at 16:06

1 Answers1

0

Here is a base R solution.

toss <- function(n = 500L){
  a <- runif(2, min = 0.3, max = 0.7)
  alpha <- a[1]
  beta <- a[2]
  x <- integer(n)
  x[1] <- rbinom(1, size = 1, prob = alpha)
  for(i in seq_len(n - 1)){
    if(x[i] == 0)
      x[i + 1L] <- rbinom(1, size = 1, prob = 1 - alpha)
    else
      x[i + 1L] <- rbinom(1, size = 1, prob = 1 - beta)
  }
  list(x = x, alpha = alpha, beta = beta)
}

set.seed(2021)
X <- toss()
table(X$x)
#
#  0   1 
#277 223 
mean(X$x)
#[1] 0.446
X$alpha
#[1] 0.4805069
X$beta
#[1] 0.6135119

Histogram of 1000 runs.

To run the function repeatedly, use replicate.

Y <- replicate(1000, mean(toss()$x))
hist(Y, xlab = "Proportion of successes")

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Hello, thanks for helping out. I have to ask though what is the purpose of set.seed(2021). Sorry for asking because I have no idea what it does and why the number 2021. Secondly, I believed that you wanted to type out Return(x) at the end ? – Jose Moquiambo Oct 28 '21 at 11:45
  • @user17233361 `set.seed` is meant to start the random number generator sequence always from the same point, to make the results reproducible. As for `return(x)`, there's no need for an explicit call to the function `return()`, the last instruction of an R function is its return value. – Rui Barradas Oct 28 '21 at 11:54
  • By the way, is there any other alternative way I can code "for(i in seq_len(n - 1))". Secondly, could you tell me what does seq_len does in this context? Thank you very much – Jose Moquiambo Oct 28 '21 at 14:39
  • @user17233361 `seq_len(n - 1)` creates a sequence of integers from 1 with length `n-1`. It's safer than `1:(n-1)` because if `n<-0`, then `1:(n-1) == 1:-1 == 1, 0, -1`. And if `n<-1`, then `1:(n-1) == 1:0 == 1, 0`. I suggest you should code it as `seq_len`, don't look for alternatives. – Rui Barradas Oct 28 '21 at 14:49
  • Firstly, thank you for the help but could you tell me how do I check for the value of alpha and beta that was used to find mean(x) and hist(x). – Jose Moquiambo Oct 29 '21 at 14:31
  • @user17233361 Done, see the edit. The function now returns a list with `x`, `alpha` and `beta`. – Rui Barradas Oct 29 '21 at 14:58
  • Hello there sorry for bothering you. I thought that I had this solved but apparently I need to find plot a histogram of the result where the y label is the frequency and the x label is the proportion of the number of 0s to 500. A friend of mine told me that you need to run 100s of samples to get enough proportions. – Jose Moquiambo Nov 01 '21 at 14:59
  • @user17233361 Done, see the edit. – Rui Barradas Nov 01 '21 at 15:08
  • It says "Error in integer(n) : invalid 'length' argument" when i tried to create the Replicate function – Jose Moquiambo Nov 01 '21 at 15:45
  • @user17233361 Are you running the function as posted? The error comes from the function, instruction `x<-integer(n)`, not from `replicate`. I am not able to reproduce that error. – Rui Barradas Nov 01 '21 at 15:50
  • Alright it works, It was stupid of me to add X into the bracket of mean(CT(X)$x)) – Jose Moquiambo Nov 01 '21 at 15:58