Quantcast

linear regression by column

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

linear regression by column

Filoche
Hi everyone.

I have a DF with the first column being my independant variable and all other columns the dependent variables.

Something like:

x y1 y2 y3
... ... ... ...
... ... ... ...

What I'm trying to do is to perform a linear model for each of my "y". It is pretty simple with loops, but I'm trying to vectorize it using apply.

For instance, I tried something like:

apply(DF, 1, function(DF){lm(DF[,1] ~ Band1[,2:5])})

But apparently it does not work.

Any help would be greatly appreciated.


Phil
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: linear regression by column

David Winsemius

On Feb 29, 2012, at 1:53 PM, Filoche wrote:

> Hi everyone.
>
> I have a DF with the first column being my independant variable and  
> all
> other columns the dependent variables.
>
> Something like:
>
> x y1 y2 y3
> ... ... ... ...
> ... ... ... ...
>
> What I'm trying to do is to perform a linear model for each of my  
> "y". It is
> pretty simple with loops, but I'm trying to vectorize it using  
> *apply*.
>
> For instance, I tried something like:
>
> apply(DF, 1, function(DF){lm(DF[,1] ~ Band1[,2:5])})

apply( DF[2:5], 2, function(x){lm(DF[,1] ~ x)})

You need to use the variable name that you created in the function  
call and loop over columns, not rows.

>
> But apparently it does not work.

For about four or five reasons.

> .

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: linear regression by column

David Winsemius

On Feb 29, 2012, at 6:39 PM, David Winsemius wrote:

>
> On Feb 29, 2012, at 1:53 PM, Filoche wrote:
>
>> Hi everyone.
>>
>> I have a DF with the first column being my independant variable and  
>> all
>> other columns the dependent variables.
>>
>> Something like:
>>
>> x y1 y2 y3
>> ... ... ... ...
>> ... ... ... ...
>>
>> What I'm trying to do is to perform a linear model for each of my  
>> "y". It is
>> pretty simple with loops, but I'm trying to vectorize it using  
>> *apply*.
>>
>> For instance, I tried something like:
>>
>> apply(DF, 1, function(DF){lm(DF[,1] ~ Band1[,2:5])})
>
> apply( DF[2:5], 2, function(x){lm(DF[,1] ~ x)})
>
> You need to use the variable name that you created in the function  
> call and loop over columns, not rows.

I read the request wrong. It would be:

apply( DF[2:5], 2, function(y){y ~ DF$x)})


>
>>
>> But apparently it does not work.
>
> For about four or five reasons.
>
>> .
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: linear regression by column

Peter Ehlers
On 2012-02-29 15:45, David Winsemius wrote:

>
> On Feb 29, 2012, at 6:39 PM, David Winsemius wrote:
>
>>
>> On Feb 29, 2012, at 1:53 PM, Filoche wrote:
>>
>>> Hi everyone.
>>>
>>> I have a DF with the first column being my independant variable and
>>> all
>>> other columns the dependent variables.
>>>
>>> Something like:
>>>
>>> x y1 y2 y3
>>> ... ... ... ...
>>> ... ... ... ...
>>>
>>> What I'm trying to do is to perform a linear model for each of my
>>> "y". It is
>>> pretty simple with loops, but I'm trying to vectorize it using
>>> *apply*.
>>>
>>> For instance, I tried something like:
>>>
>>> apply(DF, 1, function(DF){lm(DF[,1] ~ Band1[,2:5])})
>>
>> apply( DF[2:5], 2, function(x){lm(DF[,1] ~ x)})
>>
>> You need to use the variable name that you created in the function
>> call and loop over columns, not rows.
>
> I read the request wrong. It would be:
>
> apply( DF[2:5], 2, function(y){y ~ DF$x)})

Another possibility: from ?lm:

"If response is a matrix a linear model is fitted separately by
  least-squares to each column of the matrix."

Peter Ehlers

>
>
>>
>>>
>>> But apparently it does not work.
>>
>> For about four or five reasons.
>>
>>> .
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> [hidden email] 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: linear regression by column

Filoche
Thank everyone for your help.

Problem solved. I'm getting more used with vectorization with your help,

Regards,
Phil
Loading...