[R] covar

rex.dwyer at syngenta.com rex.dwyer at syngenta.com
Thu Feb 17 17:43:59 CET 2011


I hate to sound like David "Have You Read The Posting Guide?" Winsemius, but there's no way for anyone to know what you are trying to accomplish here without a lot more information.  You don't show us the output you expect and the output you got.  I would expected "relatedness" to be on a scale from 0 to 1, but it's clear that you'll get values >1 in this program.
To use R effectively, you need to rephrase your computation as a matrix computation.  People generally use R at least partly to avoid debugging index computations in for-loops.  For-loops are also much slower than the corresponding matrix operations in R.  If you want to use for-loops, you can always put in some prints and trace what's going on, just like the old days!

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Val
Sent: Wednesday, February 16, 2011 3:14 PM
To: r-help at stat.math.ethz.ch
Subject: [R] covar

Hi all,

I want to construct relatedness among individuals and have a look at the
following script.

#########################
rm(list=ls())

N=5
id   = c(1:N)
dad = c(0,0,0,3,3)
mom  = c(0,0,2,1,1)
sex  = c(2,2,1,2,2) # 1= M and 2=F

   A=diag(nrow = N)
   for(i in 1:N)    {
      for(j in i:N)      {
         ss = dad[j]
         dd = mom[j]
         sx = sex[j]
          if( ss > 0 && dd > 0 )
            {
              if(i == j)
                   { A[i,i] = 1 + 0.5*A[ss,dd] }
                 else
                  { A[i,j] = A[i,ss] + 0.5*(A[i,dd])
                    A[j,i] = A[i,j] }
            }

      } #inner for loop
     } # outer for loop
  A

If the sex is male ( sex=1)  then I want to set A[i,i]=0.5*A[ss,dd]
If it is female ( sex=2) then A[i,i] = 1 + 0.5*A[ss,dd]


How do I do it ?

I tried several cases but it did not work from me. Your assistance is
highly  appreciated  in advance

Thanks

        [[alternative HTML version deleted]]

______________________________________________
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.




message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited. 


More information about the R-help mailing list