[R] rolling regression between adjacent columns
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jul 30 21:00:12 CEST 2008
I was hoping for something reproducible. I can't run your code or make
use of the data to save time in creating an example. Try this with
builtin anscombe:
> nc <- ncol(anscombe)
> sapply(seq(1, nc, 2), function(i) coef(lm.fit(cbind(1, anscombe[,i]), anscombe[,i+1])))
[,1] [,2] [,3] [,4]
x1 0 13.5 1.8749414 8.6670365
x2 1 -0.5 0.7500381 -0.1554836
On Wed, Jul 30, 2008 at 2:08 PM, rcoder <mpdotbook at gmail.com> wrote:
>
> Well, in this case I don't think my original code would have helped much...
>
> So, I've rewritten as below. I want to perform regression between one column
> in a matrix and all other columns in the same matrix. I have a for loop to
> achieve this, which succeeds in exporting intercept and slope coefficients
> to a results matrix, except when a column that contains only NAs is reached.
> Columns partially filled with NAs are handled, but the code exits with
> errors when a single column is filled with NAs. I inserted the
> 'na.action=NULL' statement within the lm() construct, but to no avail. I
> would be very grateful for any advice.
>
>>tt<-time(SourceMat)
>>ResultMat<-matrix(NA, ncol=colnum, nrow=rownum) #creates an o/p
> template matrix
>
> #loop through each column in the source matrix:
>>for (i in 1:5000)
> {
> sel_col<-[col(SourceMat)==i] #selecting the correct column in the
> matrix in turn
> SourceMat[,i]<-coef(lm(tt~sel_col), na.action=NULL)
> }
>
> Thanks,
>
> rcoder
>
>
> Gabor Grothendieck wrote:
>>
>> Read the last line of every message to r-help.
>>
>> On Tue, Jul 29, 2008 at 6:15 PM, rcoder <mpdotbook at gmail.com> wrote:
>>>
>>> Hi everyone,
>>>
>>> I am trying to apply linear regression to adjacent columns in a matrix
>>> (i.e.
>>> col1~col2; col3~col4; etc.). The columns in my matrix come with
>>> identifiers
>>> at the top of each column, but when I try to use these identifiers to
>>> reference the columns in the regression function using rollapply(), the
>>> columns are not recognised and the regression breaks down. Is there a
>>> more
>>> robust way to reference the columns I need, so that I can apply the
>>> regression across the matrix; 'by.column', but every other column?
>>>
>>> Thanks,
>>>
>>> rcoder
>>> --
>>> View this message in context:
>>> http://www.nabble.com/rolling-regression-between-adjacent-columns-tp18722392p18722392.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> ______________________________________________
>> 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.
>>
>>
>
> --
> View this message in context: http://www.nabble.com/rolling-regression-between-adjacent-columns-tp18722392p18739292.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list