[R] apply regression to an array

Adrienne Wootten amwootte at ncsu.edu
Tue Oct 6 20:42:30 CEST 2015


Almost forgot that function lmfunc is this:

lmfunc = function(valist,input){
  fitted.values(lm(valist~input))
}

A


On Tue, Oct 6, 2015 at 2:41 PM, Adrienne Wootten <amwootte at ncsu.edu> wrote:

> FYI I did try something like this:
>
> test = apply(test3,c(1,2),lmfunc,input=t)
>
> but that gives me an array that is 10 rows by 5 columns by 5 slices, and I
> need it to keep the same dimensions as test3 (5x5x10)
>
> A
>
> On Tue, Oct 6, 2015 at 1:42 PM, Adrienne Wootten <amwootte at ncsu.edu>
> wrote:
>
>> R-Helpers,
>>
>> I've seen some similar threads about this question online, but not quite
>> what I'm looking for.  I apologize in advance if someone's already answered
>> this and I just can't find it online.
>>
>> Say that I have an array like test3 in the little example code I have
>> below:
>>
>> test1 = array(rep(1:10,each = 25),dim=c(5,5,10))
>> test2 = array(rnorm(250,0,0.35),dim=c(5,5,10))
>> test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices
>>
>> time=1:10
>>
>> Where the dimensions are x, y, and time.  What I'd like to do is run a
>> regression (for the sake of this example, say lm) on each x,y in time.  So
>> for a single cell the formula might be test3[1,1,]~time, but I'd like to
>> that for all cells.  The only way I can immediately think of is to use a
>> loop, but I'm wondering if there's a way to do this without a loop.
>> Perhaps with tapply?
>>
>> I'm actually doing a fourth order regression with a much larger array,
>> but this simple example illustrates the question I have.
>>
>> Many thanks for the help! Sorry if someone's already answered this and I
>> can't find it.
>>
>> Adrienne
>>
>> --
>> Adrienne Wootten
>> Graduate Research Assistant
>> State Climate Office of North Carolina
>> Department of Marine, Earth and Atmospheric Sciences
>> North Carolina State University
>>
>
>
>
> --
> Adrienne Wootten
> Graduate Research Assistant
> State Climate Office of North Carolina
> Department of Marine, Earth and Atmospheric Sciences
> North Carolina State University
>



-- 
Adrienne Wootten
Graduate Research Assistant
State Climate Office of North Carolina
Department of Marine, Earth and Atmospheric Sciences
North Carolina State University

	[[alternative HTML version deleted]]



More information about the R-help mailing list