Dear R-users I have four simple linear models, which are all in the form of a*X+b The estimated parameter vectors are a <- c(1,2,3,4) b <- c(4,3,2,1) My goal is to draw a plot where x-axis is X (range from -100 to 100) and y-axis is the sum of all four linear models X <- seq(-100,100, length=10000) plot(X, sum of the four linear functions) I started with a function for summing summing <- function(a,b) {temp <-0; for (i in 1:length(a)) temp <- temp + a[i]*X+b[i] } plot(X, summing(a,b)) I am a R beginner. I am looking for a more efficient R code. Any help or advice will be appreciated. Taka,
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] > On Behalf Of Taka Matzmoto > Sent: Monday, June 26, 2006 5:21 PM > To: r-help at stat.math.ethz.ch > Subject: [R] looking for a more efficient R code. > > Dear R-users > > I have four simple linear models, which are all in the form of a*X+b > The estimated parameter vectors are > > a <- c(1,2,3,4) > b <- c(4,3,2,1) > > My goal is to draw a plot where x-axis is X (range from -100 to 100) and > y-axis is the sum of > all four linear models > > X <- seq(-100,100, length=10000) > plot(X, sum of the four linear functions) > > I started with a function for summing > > summing <- function(a,b) > {temp <-0; > for (i in 1:length(a)) > temp <- temp + a[i]*X+b[i] > } > > plot(X, summing(a,b)) > > I am a R beginner. I am looking for a more efficient R code. > > Any help or advice will be appreciated. > > Taka, >Taka, I won't claim more efficient, that is for you to decide. But you might try something like: plot(x, colSums(t(x %*% t(b)) + a)) Dan Daniel Nordlund Bothell, WA
try this: a <- c(1,2,3,4) b <- c(4,3,2,1) X <- seq(-100,100, length=10000) z <- colSums(sapply(X,function(x)a*x+b)) plot(X, z, type='l') On 6/26/06, Taka Matzmoto <sell_mirage_ne@hotmail.com> wrote:> > Dear R-users > > I have four simple linear models, which are all in the form of a*X+b > The estimated parameter vectors are > > a <- c(1,2,3,4) > b <- c(4,3,2,1) > > My goal is to draw a plot where x-axis is X (range from -100 to 100) and > y-axis is the sum of > all four linear models > > X <- seq(-100,100, length=10000) > plot(X, sum of the four linear functions) > > I started with a function for summing > > summing <- function(a,b) > {temp <-0; > for (i in 1:length(a)) > temp <- temp + a[i]*X+b[i] > } > > plot(X, summing(a,b)) > > I am a R beginner. I am looking for a more efficient R code. > > Any help or advice will be appreciated. > > Taka, > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What is the problem you are trying to solve? [[alternative HTML version deleted]]
Little algebra will convince you that your 'summing' function is just equal to: sum(a) + sum(b)*X So, it is very difficult to get it any faster than: plot(X, sum(a)+sum(b)*X) And, by the way, most of the execution time is spent plotting, not computing the average:> system.time({y1 <- summing(a,b)})[1] 0.002 0.000 0.001 0.000 0.000> system.time({y2 = sum(a)+sum(b)*X})[1] 0 0 0 0 0> all.equal(y1,y2)[1] TRUE> system.time(plot(X,y2))[1] 0.019 0.002 0.051 0.000 0.000>Gardar On Mon, 2006-06-26 at 17:21, Taka Matzmoto wrote:> Dear R-users > > I have four simple linear models, which are all in the form of a*X+b > The estimated parameter vectors are > > a <- c(1,2,3,4) > b <- c(4,3,2,1) > > My goal is to draw a plot where x-axis is X (range from -100 to 100) and > y-axis is the sum of > all four linear models > > X <- seq(-100,100, length=10000) > plot(X, sum of the four linear functions) > > I started with a function for summing > > summing <- function(a,b) > {temp <-0; > for (i in 1:length(a)) > temp <- temp + a[i]*X+b[i] > } > > plot(X, summing(a,b)) > > I am a R beginner. I am looking for a more efficient R code. > > Any help or advice will be appreciated. > > Taka, > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html