[R] loop troubles

David Winsemius dwinsemius at comcast.net
Sun Jul 18 22:41:09 CEST 2010


On Jul 18, 2010, at 4:21 PM, Joe P King wrote:

> This is the latest code I have been trying, but it when I try to  
> turn the
> vectors of SD into a vector of variances, it turns into a scalar.
>
> n=c(NULL,13,89,50)
> ybar=c(NULL,1.58,1.26,0.80)
> sd=c(NULL,2.19,4.18,5.47)
> mu=1;sigma=1;p0=0;var1=NULL;p=NULL;pn=NULL

If you are going to passing values to the above using indices then  
they should be defined as vectors of the appropriate length. It would  
not be absolutely necessary to to so , but if you don't, then you need  
to extend them using the c() function, which you do not seem to have  
caught onto yet.


> for (i in 2:length(n)){
>  var1[i] = sd[i]^2 #sample variance
>  p[i] = n[i]/var1[i] #sample precision
>  p0=p[i-1]
>  pn[i] = p0[i]+p[i]
>  mu[i] = ((p0[i])/(pn[i])*mu[i])+((p[i])/(pn[i]))*ybar[i]
>  sigma[i] = 1/sqrt(pn[i])
>  mutotals[i,]<-mu[i]
> }
>
> Joe King
> 206-913-2912
> jp at joepking.com
> "Never throughout history has a man who lived a life of ease left a  
> name
> worth remembering." --Theodore Roosevelt
>
>
>
> -----Original Message-----
> From: Joe P King [mailto:jp at joepking.com]
> Sent: Sunday, July 18, 2010 6:13 AM
> To: 'r-help at r-project.org'
> Subject: RE: [R] loop troubles
>
> I tried that, this is what I tried, but I only get it to do one  
> iteration
> and then it wont cycle back. I am not sure how to tell it how to  
> look at the
> previous precision to add to the current new sample precision to get  
> the new
> precision. This is my first try at a loop so I am not sure if I am  
> doing
> anything right, sorry about the elementary question.
>
> n=c(5,10,15)
> ybar=c(12,13,14)
> sd=c(2,3,4)
> mutotals<-matrix(nrow=length(n), ncol=1)
> mu=1;p0=0
> for (i in 1:length(n)){
>  var = sd[i]^2 #sample variance
>  p = n[i]/var #sample precision
>  p0[i]= i
>  pn = p0[i]+p[i]
>  mu.out = ((p0[i])/(pn)*mu[i])+((p)/(pn))*ybar[i]
>  sigma[i] = 1/sqrt(pn[i])
>  mutotals[i,]<-mu.out[i]
> }
>
> Joe King
> 206-913-2912
> jp at joepking.com
> "Never throughout history has a man who lived a life of ease left a  
> name
> worth remembering." --Theodore Roosevelt
>
>
>
> -----Original Message-----
> From: David Winsemius [mailto:dwinsemius at comcast.net]
> Sent: Sunday, July 18, 2010 5:36 AM
> To: Joe P King
> Cc: r-help at r-project.org
> Subject: Re: [R] loop troubles
>
>
> On Jul 17, 2010, at 9:09 PM, Joe P King wrote:
>
>> Hi all, I appreciate the help this list has given me before. I have a
>> question which has been perplexing me. I have been working on doing a
>> Bayesian calculating inserting studies sequentially after using a
>> non-informative prior to get a meta-analysis type result. I created a
>> function using three iterations of this, my code is below. I insert
>> prior
>> mean and precision (I add precision manually because I want it to be
>> zero
>> but if I include zero as prior variance I get an error cant divide
>> by zero.
>> Now my question is instead of having the three iterations below, is
>> there
>> anyway to create a loop, the only problem is I want to have elements
>> from
>> the previous posterior to be the new prior and now I cant figure out
>> how to
>> do the code below in a loop.
>
> Perhaps if you set the problem up with vectors, it would become more
> obvious how to do it with indexing or loops:
>
> n <-  c(5, 10, 15)
> ybar <- c(12,12,14)  # and so on...
>
> -- 
> David
>> The data below is dummy data, I used a starting
>> mu of 1, and starting precision of 0.
>>
>>
>>
>> bayes.analysis.treat<-function(mu0,p0){
>>
>> n1 = 5
>>
>> n2 = 10
>>
>> n3 = 15
>>
>> ybar1 = 12
>>
>> ybar2 = 13
>>
>> ybar3 = 14
>>
>> sd1 = 2
>>
>> sd2 = 3
>>
>> sd3 = 4
>>
>> #posterior 1
>>
>> var1 = sd1^2 #sample variance
>>
>> p1 = n1/var1 #sample precision
>>
>> p1n = p0+p1
>>
>> mu1 = ((p0)/(p1n)*mu0)+((p1)/(p1n))*ybar1
>>
>> sigma1 = 1/sqrt(p1n)
>>
>> #posterior 2
>>
>> var2 = sd2^2 #sample variance
>>
>> p2 = n2/var2 #sample precision
>>
>> p2n = p1n+p2
>>
>> mu2 = ((p1n)/(p2n)*mu1)+((p2)/(p2n))*ybar2
>>
>> sigma2 = 1/sqrt(p2n)
>>
>> #posterior 3
>>
>> var3 = sd3^2 #sample variance
>>
>> p3 = n3/var3 #sample precision
>>
>> p3n = p2n+p3
>>
>> mu3 = ((p2n)/(p3n)*mu2)+((p3)/(p3n))*ybar3
>>
>> sigma3 = 1/sqrt(p3n)
>>
>> posterior<-cbind(rbind(mu1,mu2,mu3),rbind(sigma1,sigma2,sigma3))
>>
>> rownames(posterior)<-c("Posterior 1", "Posterior 2", "Posterior 3")
>>
>> colnames(posterior)<-c("Mu", "SD")
>>
>> return(posterior)}
>>
>>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list