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

Reply via email to