# [R] How to extract residuals from multiple regressions from a loop

William Dunlap wdunlap at tibco.com
Mon Oct 28 16:33:44 CET 2013

```Can you trim down your example to a size where you can show us
the data (using dump() or dput()) and the commands you used so
one can just copy it from your mail and paste it into R to reproduce
the problem?

I don't see your problem when I made up data similar to what you described:

> timeseries <- ts(cbind(c(NA, 2:9, NA), c(1,NA,3,NA,5:10), c(rep(NA,9), 10)))
> exc.mkt <- log(1:10)
> for(i in 1:ncol(timeseries)) print(length(residuals(lm(timeseries[,i]~exc.mkt, na.action=na.exclude))))
[1] 10
[1] 10
[1] 10
> # without na.action=na.exclude residuals do vary in length
> for(i in 1:ncol(timeseries)) print(length(residuals(lm(timeseries[,i]~exc.mkt))))
[1] 8
[1] 8
[1] 1

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Ben Ammar
> Sent: Monday, October 28, 2013 6:20 AM
> To: r-help at r-project.org
> Subject: [R] How to extract residuals from multiple regressions from a loop
>
>
>    Dear all
>
>    I've  got  the following problem, I want to extract the residuals from
>    regression loops. The problem here is that some columns include NA's at the
>    beginning and end (i.e. each time series of stocks starts at different
>    points  in  time and ends at different points in time). When I want to
>    transfer these residuals into a matrix to determine the residual matrix, I
>    get the error message ("number of items to replace is not a multiple of
>    replacement length"). I tried it with na.action=na.exclude but that doesn't
>    work because that command doesn't actually change the vector length. With a
>    loop I came this far:
>    Number of stocks is 50 and maximum time period is 258 months:
>
>    for (i in 1:50) {CAPM.res[,i] <- residuals(lm(timeseries[,i]~exc.mkt),
>    na.action=na.exclude)}
>
>    as I said it doesn't work because of the different column length in the
>    matrix "timeseries". So right now I'm doing kind of manually which works
>    perfectly but is quite intensive and looks like that:
>    test.1 <- lm(timeseries[,1]~exc.mkt, na.action=na.exclude)
>    residual.test.1 <- residuals(test.1)
>    CAPM.res[,1] <- residual.Life.1
>
>    test.2 <- lm(timeseries[,2]~exc.mkt, na.action=na.exclude)
>    residual.test.2 <- residuals(test.2)
>    CAPM.res[,2] <- residual.test.2
>
>    ....and so on for the remaining 49 stocks. When I look at that I obviously
>    see that this must be done with a loop but in the end I can't put in the
>    matrix because of the different lengths. So far I got this:
>    test<-matrix(0,50,258)
>    residual.test<-matrix(0,50,258)
>    for (i in 1:50) {lm(timeseries[,i]~exc.mkt, na.action=na.exclude)
>                    {residual.test[i] <- residuals(test[i])
>                    {CAPM.res[,i] <- residual.test[i]
>    }}}
>
>    but here I get the error message: "Error: \$ operator is invalid for atomic
>    vectors"
>    and I don't think "test"  and "residual.test" is defined correctly because I
>    don't know where to look for the residuals.
>
>    Does anyone have an idea how to extract the residuals and put them in a
>    258x50 matrix?
>    Any help would be very much appreciated!
>
>    Cheers,
>    Ben
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help