Bill, Thanks a bunch that works great!
A On Tue, Oct 6, 2015 at 2:56 PM, William Dunlap <wdun...@tibco.com> wrote: > Since the model matrix, cbind(1,time) is the same for all your > response variables, > you can calculate this on one call to lm, but you have to rearrange the > response > values so that each x,y set is in one column. I think the following > function does it: > > f <- function (time, y) > { > stopifnot(length(dim(y)) == 3, dim(y)[3] == length(time)) > yMatrix <- matrix(aperm(y, c(3, 1, 2)), dim(y)[3]) > fit <- lm(yMatrix ~ time) > aperm(array(fitted.values(fit), dim(y)[c(3, 1, 2)]), c(2, > 3, 1)) > } > > E.g., > > fitted.values(lm(test1[2,5,]~time)) > 1 2 3 4 5 6 7 8 9 10 > 1 2 3 4 5 6 7 8 9 10 > > f(time, test1)[2,5,] > [1] 1 2 3 4 5 6 7 8 9 10 > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > > On Tue, Oct 6, 2015 at 10:42 AM, Adrienne Wootten <amwoo...@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 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > -- 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]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.