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]]
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 [[alternative HTML version deleted]]
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]]
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 <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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Bill, Thanks a bunch that works great! A On Tue, Oct 6, 2015 at 2:56 PM, William Dunlap <wdunlap at 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 <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 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at 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]]
On Oct 6, 2015, at 10:42 AM, Adrienne Wootten 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?Would not be expecting a 5x5x10 results since you are using the last dimension to calculate a two parameters for each row and col. Why not use a loop? Doing it with an index is just a a disguised loop: apply( test3, 1:2, function(x) coef(lm(x~time) ) ) # iterates over rows and cols. # results is 5 x 5 x2> > 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 at 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.David Winsemius Alameda, CA, USA