How to calculate the probability problem?

The problem is just to calculus the P(y<=r) r =0, 1 .... n for every case of n and p that i inputed in the vec?

  • The problem is to compare the Poisson and Central Limit Theorem approximation for P(Y<=r), r=0,1,...n n=10, 20, 30...(increasing by 10s) 90,100, 200, (increasing by 100) and possibly up to 1000 p=.001, .002 ...(increasing by .001) .... 009, .01, .02...(increasing by .01) .09,.1,.2(increasing by .1)up to .5 HERE IS MY CODE SO FAR: ### n and p n<-c( seq ( 10 , 100 , 10 ), seq ( 200 ,900 ,100 ) ) p<-c(seq( .001 , .01 , .001 ), seq( .01 , .1, .01 ), seq( .1, .5, .1)) #define print probability p_binomial=0 #counts poissoncount = 0 CLTcount = 0 #binomial for(i in 1:length(n)){ for(j in 1:length(p)){ for(k in 1:n[i]){ #calculate probability for every n and p y<-rbinom(n[i],k,p[j]) p_binomial=length(y[y<=k])/length(y) #print (p_binomial) #calculate normal p_normal = rnorm(k, mean = n[i]*p[j], sqrt(n[i]*p[j]*(1-p[j]))) #print (p_normal) #calculate lambda #poisson lambda = n*p p_poisson = ppois(k, lambda, lower.tail = TRUE, log.p = FALSE) #print (p_poisson) #Compare Poisson and Binomial PdifB= abs(p_poisson - p_binomial) #compare CLT to binomial NdifB= abs(p_normal - p_binomial) # see which is greater if (NdifB > PdifB){ #print("Poisson") poissoncount=poissoncount+1 } if(NdifB<PdifB){ #print("CLT") CLTcount= CLTcount+1 } else #print ("same") } } } edit: the problem is just to calculus the P(y<=r) r =0, 1 .... n for every case of n and p that i inputed in the vectors i'm pretty sure that the probability codes p binomial, normal, poisson calculate the correct probability, although that's not my main concern right now. when i run it it will tell me "same" "CLT" or "poisson" but it doesn't increment when i try to count how many times poisson is more accurate than CLT and vice versa ok sorry i meant that it's for a stats class because i'm not good at programming so please help :(

  • Answer:

    Dear BB, Are you not seeing that R reports errors to you when you try running your program? These occur near the end of your code, and say things like "Error: unexpected '}' in "}"" (which show in blue text, in contrast to the red text of your code). As I've stated before, if you have syntax errors within your loops (which you clearly do), then of course the loops will not increment because R cannot run them with these errors. R will only run the initial portion of your code prior to your first loop. Correcting the syntax errors is the easy part. Do you see that the "else" near the end of your code dangles there without any use? It is not what R expects (R is very finicky about the use of "else" and wants it to be more tightly bound to the end of the "if" clause), and because this else is useless, it can simply be removed instead of fixed. Once you correct the syntax errors caused by the misplaced "else," the loops should increment. However, that doesn't mean that they will produce the results that you want. It only means that they will produce the results you have instructed. (Computers are stupid in that way, as they do exactly as they are told.) To make the body of your loops do what you want, you have to be very precise in making your instructions align with your desires. When doing that, I suggest that you start by disabling your loops and manually setting variables "i," "j," and "k" to some fixed values (I suggest starting with all three variables set to 3). Then run the code within the body of the loops, one line at a time. Check to see that what each line produces matches what you expect it to produce BEFORE running the next line of code. When you find something unexpected (which I promise you will), then you need to understand that your instruction is flawed and needs to be replaced with something that produces what you actually want. Once you have determined that every line of code within the loop is giving you what you want, then repeat this process for various different values "i," "j," and "k." Only after doing these steps should you reinstate the loops. I'll gladly help you more if you get stuck and have specific questions. However, I think that for you to carefully check your calculations a few times will significantly enhance your understanding, both of probability and of R. You have fewer than a dozen executable lines of code within your loops, which are not very complicated and which you have written yourself, so this is not like needing to verify and debug a large, complex program written by someone else. Also, don't forget to pay attention to errors and warnings that you receive from R. Remember you are dealing with cause and effect, so you usually should investigate the reason(s), or cause(s), that produce those messages. EDIT: I also see that when you assign "p" (prior to your loops), you duplicate 0.01 and 0.1, which conflicts with the introductory description in your question. In case that is not what you want, then you might instead use something like the line below. p <- c(seq(0.001, 0.01, 0.001), seq(0.02, 0.1, 0.01), seq(0.2, 0.5, 0.1))

BB at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.