[R] lm() of one matrix against another
baptiste Auguié
ba208 at exeter.ac.uk
Sun Apr 13 12:38:18 CEST 2008
Thanks for both replies, here is the cleaner version,
>
> n<-5
> N<-10
>
> data.x<-matrix(1:(n*N),ncol=n)
> data.y<-matrix(1:(n*N) + rnorm(n*N,sd=1),ncol=n)
>
> matplot(data.x,data.y,t="p",pch=1:n,bty="n")
>
> mapply(function(x,y){res <- lm(y~x) ; lines(x,predict(res)) ; res} ,
> as.data.frame(data.x),as.data.frame(data.y),SIMPLIFY=F)
Out of curiosity, I'm not sure if one could get the lines colored
corresponding to their index within mapply (or any function of the
apply family for that matter). Is there a special trick to refer to
the index under evaluation?
Thanks again,
baptiste
On 12 Apr 2008, at 20:08, Henrique Dallazuanna wrote:
> Try
> mapply(function(x,y)lm(y~x),as.data
> .frame(data.x),as.data.frame(data.y),SIMPLIFY=F)
>
> 2008/4/12, baptiste Auguié <ba208 at exeter.ac.uk>:
>> Hello R list,
>>
>>
>> I have two matrices of identical dimensions, and I want to fit a
>> straight line for each pair of columns and plot the resulting lines.
>> I got it to work with a for loop, but there must be a better way,
>>
>>
>>> n<-5
>>> N<-10
>>>
>>> data.x<-matrix(1:(n*N),ncol=n)
>>> data.y<-matrix(1:(n*N) + rnorm(n*N,sd=1),ncol=n)
>>>
>>> matplot(data.x,data.y,t="p",pch=1:n,bty="n")
>>>
>>> for (ii in 1:n)
>>> {
>>> test <- lm(y~x,data=list(x=data.x[,ii],y=data.y[,ii]) )
>>> lines(data.x[,ii],test$coefficients[1] + test$coefficients[2] *
>>> data.x[,ii],lty=2,col=ii)
>>> }
>>
>>
>>
>> Thanks,
>>
>> baptiste
>>
>>
>> _____________________________
>>
>> Baptiste Auguié
>>
>> Physics Department
>> University of Exeter
>> Stocker Road,
>> Exeter, Devon,
>> EX4 4QL, UK
>>
>> Phone: +44 1392 264187
>>
>> http://newton.ex.ac.uk/research/emag
>> http://projects.ex.ac.uk/atto
>>
>> ______________________________________________
>> 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.
>>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
_____________________________
Baptiste Auguié
Physics Department
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
http://projects.ex.ac.uk/atto
More information about the R-help
mailing list