Skip to content
Back to formatted view

Raw Message

Message-ID: <CAM_vju=ctZa3dvMJ3iaf+Zdmg3GoQgTha4tDspwFoxFuR+YdBQ@mail.gmail.com>
Date: 2012-08-01T13:13:18Z
From: Sarah Goslee
Subject: help with a regression problem
In-Reply-To: <8F4C481EBCA71E45845A4CD0778F60E4045432CC@exmbx2010-8.campus.MCGILL.CA>

Hi,

On Wed, Aug 1, 2012 at 9:06 AM, R Heberto Ghezzo, Dr
<heberto.ghezzo at mcgill.ca> wrote:
> Hello,
> I have a big data frame where consecutive time dates and corresponding observed values for each subject (ID) are on a line. I want to compute the linear slope for each subject. I would like to use apply but I do
> not know how to express the corresponding function. An example using a loop follows
> #
> # create dummy data set There are missing values
>  a <- c(1,2,3,4, 1,1,1,1, 2,2,3,3, 3,4,NA,4, 5,5,5,5,
>             2.1,2.2,2.3,2.4, 2.3,2.4,2.6,2.6, 2.5,2.6,2.9,3,
>             2.6,NA,3.2,4)
> a <- matrix(a, nr=4)
> aa <- as.data.frame(a)
> names(aa) <- c("ID","X1","X2","X3","X4","Y1","Y2","Y3","Y4")
> #
> #  I want the regression coefficientes of the Y on the X for each ID
> #
> sl <- rep(NA,4)
> for(i in 1:4) {
>   x1 <- a[i,2:5]
>   y1 <- a[i,6:9]
>   sl[i] <- lm(y1 ~ x1)$coef[2]
> }
> sl
> #
> #   I would like to use apply on the data.frame aa but with which function?
> #
> sl <- apply(aa,1,FUN) # FUN = ??

You could do it as a one-liner, but it's a lot more understandable if
you write your own function.

myfun <- function(a) {
   x1 <- a[2:5]
   y1 <- a[6:9]
   lm(y1 ~ x1)$coef[2]
}

Then you can pass that function to apply:
sl <- apply(aa,1,myfun)


Sarah



-- 
Sarah Goslee
http://www.functionaldiversity.org