s-news
[Top] [All Lists]

Re: error using 'by' - thankyou

To: s-news@lists.biostat.wustl.edu
Subject: Re: error using 'by' - thankyou
From: susan@stats.ucl.ac.uk
Date: Wed, 16 Feb 2005 20:34:52 -0000 (GMT)
Cc: s-news@lists.biostat.wustl.edu
Importance: Normal
In-reply-to: <42139446.8000209@pdf.com>
References: <2254.213.107.144.164.1108578940.squirrel@213.107.144.164> <42139446.8000209@pdf.com>
User-agent: SquirrelMail/1.4.3a
Thanks to Sundar.
>
> susan@stats.ucl.ac.uk wrote:
>> Dear Colleagues
>>
>> I am having some difficuly with the by function. In the function DtW
>> below
>> I get the correct answer if p=1 but the error message if p=2. The
>> problem
>> seems to occur after Dt had been done. Any help would be greatly
>> appreciated and I apologies in advance if it's something trivial that I
>> missed.
>>
>> Thank you again
>> susan
>>
>> DtW <- function(data.mat, W, p)
>> {
>>      D2 <- data.mat[, 5]
>>      cls <- length(D2)
>>      D1 <- matrix(1, cls, 1)
>>      D <- D1
>>      if(p == 2) {
>>              D <- data.frame(D1, D2)
>>      }
>>      Dt <- t(D)
>>      print(Dt)
>>      DtW <- Dt %*% W
>>      DtWD <- DtW %*% D
>>      print(DtWD)
>>      DtWD
>>
>> #gen data
>> tempdat1 <- simdat(2, 3, 0, "exch", 0.5)
>> print(tempdat1)
>>      clid clsize visit        yij grp
>> [1,]  300      3     1 -0.4788661   0
>> [2,]  300      3     3 -0.3704168   0
>> [3,]  300      3     6 -1.8812922   0
>> [4,]  301      3     1 -0.4165800   1
>> [5,]  301      3     3  0.9933311   1
>> [6,]  301      3     4 -0.6556154   1
>> W3 <- matrix(2, 3, 3)
>> testdtw <- by(tempdat1, tempdat1[, 1], DtW, W = W3, p
>>       = 2)
>>    1 2 3
>> D1 1 1 1
>> D2 0 0 0
>> Error in as.double: Cannot coerce mode list to double: .Data = list(..
>>
>>
>
> Hi Susan,
>
> Yes, this is because "%*%" is expecting a matrix, not a data.frame (or
> list), which "D" is if p = 2. You should try:
>
>       if(p == 2) {
>               D <- cbind(D1, D2)
>       }
>
> /untested
>
> --sundar
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
>





<Prev in Thread] Current Thread [Next in Thread>