[R] working with loops; was: help
Uwe Ligges
ligges at statistik.uni-dortmund.de
Sun Oct 28 16:49:59 CET 2007
Please read the posting guide:
1. use a sensible subject line
2. please read the docs prior to posting messages.
Some basic rule is, e.g., to initialize objects to their final lengths
before entering loops.
osita k ezeh wrote:
> hello,
>
> please can anyone help me out. Am a new user of R
> program. Am having problem
> with this code below, not getting the expected
> results.
>
> 1. Each m, the cumulative sum should be 1.000 but the
> 2nd and 3rd m returned 2.000 and 3.000
> instead of 1.000.
Well, you say cumsum(out.prob) which is the cumulative sum of the whole
vector, not just that one for the current m. Your result would not
change when you replace cumsum by sum, BTW.
> 2. to get the LCL(m) and UCL(m) for each m base on
> these instructions
> if out.cum > 0.025 then LCL(m)= y-1
> if out.cum >0.975 then ucl(m)= y-1
> how do I code these instructions into this code.
What is LCL/UCL in general?
If you have lcl & co. already, then you can say:
if(out.cum > 0.025) lcl[m] <- y-1
if(out.cum > 0.975) ucl[m] <- y-1
Are you really sure about both ">"?
Uwe Ligges
> thanks
> Aruike
>
>
>
> pp=function(x,n,M){z=1.0;a=2.3071430;b=7.266064;H=3
>
> out.h=c()
>
> out.y=c()
>
> out.m=c()
>
> out.prob=c()
>
>
>
> for(h in 1:H){
>
> for(m in 1:M){
>
> for(y in 0:m){
>
>
>
>
> g=lgamma(m+z)+lgamma(n[h]+a+b)+lgamma(x[h]+y+a)+lgamma(n[h]+m+b-x[h]-y)
>
>
>
> g=g-lgamma(y+z)-lgamma(m-y+z)-lgamma(x[h]+a)-lgamma(n[h]+b-x[h])-
> lgamma(n[h]+m+a+b)
>
>
>
> out.h=c(out.h,h)
>
> out.y=c(out.y,y)
>
> out.m=c(out.m,m)
>
> out.prob=c(out.prob, exp(g))
>
> out.cum=c(cumsum(out.prob))
>
>
>
> Result=data.frame(out.h,out.y,out.m,out.prob,out.cum)
>
> }}}}
>
> Kings=pp(x=c(19,20), n=c(52,60),3)
>
> Kings
>
> out.h out.y out.m out.prob out.cum
>
> 1 1 0 1 0.65395431 0.6539543
>
> 2 1 1 1 0.34604569 1.0000000
>
> 3 1 0 2 0.43127277 1.4312728
>
> 4 1 1 2 0.44536308 1.8766358
>
> 5 1 2 2 0.12336415 2.0000000
>
> 6 1 0 3 0.28672775 2.2867277
>
> 7 1 1 3 0.43363507 2.7203628
>
> 8 1 2 3 0.23440955 2.9547724
>
> 9 1 3 3 0.04522764 3.0000000
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list