|
Hi,
I've a data frame like this: > as.data.frame(cbind(rnorm(1:12),rnorm(1:12))) V1 V2 1 -1.30849402 -0.52094136 2 0.96157302 0.76217871 3 -0.44223351 -1.72630871 4 -0.10432438 -1.04732942 5 -1.38748914 0.95877311 6 -0.63965975 0.65494811 7 -0.24058318 0.19496830 8 -0.11172988 1.01680655 9 0.08065333 0.22168589 10 0.25196536 0.84619914 11 -0.59536986 -0.08243074 12 1.09115054 0.49822977 I need to add two columns as result of the fitting of linear model based on a preset numbers of row. For example if I need to compute a lm each 4 rows, I get the data.frame below, where intercept1 and coeff1 is obtained from V1 and V2 of first 4 rows lm(V2 ~ V1), and so on... V1 V2 "intercept" "coeff" 1 0.6931694 0.05797771 intercept1 coeff1 2 -1.4069786 0.23983307 intercept1 coeff1 3 -1.4901708 0.45079601 intercept1 coeff1 4 0.2215696 1.87888983 intercept1 coeff1 5 -0.5828106 0.90376622 intercept2 coeff2 6 -0.7607985 0.71419938 intercept2 coeff2 7 0.1273495 0.06199312 intercept2 coeff2 8 -0.5612245 1.02223971 intercept2 coeff2 9 -0.1439178 0.92135354 intercept3 coeff3 10 -1.1011662 0.02894731 intercept3 coeff3 11 -0.4098710 -0.01231322 intercept3 coeff3 12 1.1511811 -0.63923140 intercept3 coeff3 Thanks in advance, Alfredo ______________________________________________ [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. |
|
Hi
divide and conquer Split your data frame to required portions dat<-as.data.frame(cbind(rnorm(1:12),rnorm(1:12))) dat.s<-split(dat, rep(1:4, each=3)) apply lm to each portion sapply(dat.s, function(x) coef(lm(V2~V1, x))) 1 2 3 4 (Intercept) 1.21745328 -0.7783777 -0.1736094 0.1042114 V1 0.03639838 2.6203724 1.3167433 -0.2350888 Add those values to your data frame. hint: use rep(..., each=4) Regards Petr [hidden email] napsal dne 07.07.2011 16:09:51: > Alfredo Alessandrini <[hidden email]> > Odeslal: [hidden email] > > 07.07.2011 16:09 > > Komu > > [hidden email] > > Kopie > > Předmět > > [R] coefficients lm of data.frame > > Hi, > > I've a data frame like this: > > > as.data.frame(cbind(rnorm(1:12),rnorm(1:12))) > V1 V2 > 1 -1.30849402 -0.52094136 > 2 0.96157302 0.76217871 > 3 -0.44223351 -1.72630871 > 4 -0.10432438 -1.04732942 > 5 -1.38748914 0.95877311 > 6 -0.63965975 0.65494811 > 7 -0.24058318 0.19496830 > 8 -0.11172988 1.01680655 > 9 0.08065333 0.22168589 > 10 0.25196536 0.84619914 > 11 -0.59536986 -0.08243074 > 12 1.09115054 0.49822977 > > I need to add two columns as result of the fitting of linear model > based on a preset numbers of row. > > For example if I need to compute a lm each 4 rows, I get the > data.frame below, where intercept1 and coeff1 is obtained from V1 and > V2 of first 4 rows lm(V2 ~ V1), and so on... > > > V1 V2 "intercept" "coeff" > 1 0.6931694 0.05797771 intercept1 coeff1 > 2 -1.4069786 0.23983307 intercept1 coeff1 > 3 -1.4901708 0.45079601 intercept1 coeff1 > 4 0.2215696 1.87888983 intercept1 coeff1 > 5 -0.5828106 0.90376622 intercept2 coeff2 > 6 -0.7607985 0.71419938 intercept2 coeff2 > 7 0.1273495 0.06199312 intercept2 coeff2 > 8 -0.5612245 1.02223971 intercept2 coeff2 > 9 -0.1439178 0.92135354 intercept3 coeff3 > 10 -1.1011662 0.02894731 intercept3 coeff3 > 11 -0.4098710 -0.01231322 intercept3 coeff3 > 12 1.1511811 -0.63923140 intercept3 coeff3 > > > Thanks in advance, > > Alfredo > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > 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. |
|
In reply to this post by Alfredo Alessandrini
For sure some smart cookie has a much better solution but this can do
the trick dt <- as.data.frame(cbind(rnorm(1:12),rnorm(1:12))) regs <- sapply(seq(1,9,by=4), function(x) coef(lm(dt[(x:(x+3)),1] ~ dt[(x:(x+3)),2]))) tmp <- c() for (i in 1:ncol(regs)) { tmp <- cbind(tmp, sapply(1:4, function(y) regs[,i])) } cbind(dt, t(tmp)) Cheers, Filipe -----Mensagem original----- De: [hidden email] [mailto:[hidden email]] Em nome de Alfredo Alessandrini Enviada em: quinta-feira, 7 de julho de 2011 11:10 Para: [hidden email] Assunto: [R] coefficients lm of data.frame Hi, I've a data frame like this: > as.data.frame(cbind(rnorm(1:12),rnorm(1:12))) V1 V2 1 -1.30849402 -0.52094136 2 0.96157302 0.76217871 3 -0.44223351 -1.72630871 4 -0.10432438 -1.04732942 5 -1.38748914 0.95877311 6 -0.63965975 0.65494811 7 -0.24058318 0.19496830 8 -0.11172988 1.01680655 9 0.08065333 0.22168589 10 0.25196536 0.84619914 11 -0.59536986 -0.08243074 12 1.09115054 0.49822977 I need to add two columns as result of the fitting of linear model based on a preset numbers of row. For example if I need to compute a lm each 4 rows, I get the data.frame below, where intercept1 and coeff1 is obtained from V1 and V2 of first 4 rows lm(V2 ~ V1), and so on... V1 V2 "intercept" "coeff" 1 0.6931694 0.05797771 intercept1 coeff1 2 -1.4069786 0.23983307 intercept1 coeff1 3 -1.4901708 0.45079601 intercept1 coeff1 4 0.2215696 1.87888983 intercept1 coeff1 5 -0.5828106 0.90376622 intercept2 coeff2 6 -0.7607985 0.71419938 intercept2 coeff2 7 0.1273495 0.06199312 intercept2 coeff2 8 -0.5612245 1.02223971 intercept2 coeff2 9 -0.1439178 0.92135354 intercept3 coeff3 10 -1.1011662 0.02894731 intercept3 coeff3 11 -0.4098710 -0.01231322 intercept3 coeff3 12 1.1511811 -0.63923140 intercept3 coeff3 Thanks in advance, Alfredo ______________________________________________ [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. "This message and its attachments may contain confidential and/or privileged information. If you are not the addressee, please, advise the sender immediately by replying to the e-mail and delete this message." "Este mensaje y sus anexos pueden contener información confidencial o privilegiada. Si ha recibido este e-mail por error por favor bórrelo y envíe un mensaje al remitente." "Esta mensagem e seus anexos podem conter informação confidencial ou privilegiada. Caso não seja o destinatário, solicitamos a imediata notificação ao remetente e exclusão da mensagem." ______________________________________________ [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. |
|
In reply to this post by Alfredo Alessandrini
Hi:
Here's another approach using the plyr package: library(plyr) df <- data.frame(gp = factor(rep(1:3, each = 4)), x = rnorm(12), y = rnorm(12)) mylst <- split(df, df$gp) mycoefs <- ldply(mylst, function(d) coef(lm(y ~ x, data = d))) names(mycoefs) <- c('gp', 'intercept', 'slope') merge(df, mycoefs, by = 'gp', all.x = TRUE) One could write this more compactly as dfr <- merge(df, ldply(split(df, df$gp), function(d) coef(lm(y ~ x, data = d))), by.x = 'gp', by.y = '.id', all.x = TRUE) names(dfr) <- c('x', 'y', 'intercept', 'slope') dfr HTH, Dennis On Thu, Jul 7, 2011 at 7:09 AM, Alfredo Alessandrini <[hidden email]> wrote: > Hi, > > I've a data frame like this: > >> as.data.frame(cbind(rnorm(1:12),rnorm(1:12))) > V1 V2 > 1 -1.30849402 -0.52094136 > 2 0.96157302 0.76217871 > 3 -0.44223351 -1.72630871 > 4 -0.10432438 -1.04732942 > 5 -1.38748914 0.95877311 > 6 -0.63965975 0.65494811 > 7 -0.24058318 0.19496830 > 8 -0.11172988 1.01680655 > 9 0.08065333 0.22168589 > 10 0.25196536 0.84619914 > 11 -0.59536986 -0.08243074 > 12 1.09115054 0.49822977 > > I need to add two columns as result of the fitting of linear model > based on a preset numbers of row. > > For example if I need to compute a lm each 4 rows, I get the > data.frame below, where intercept1 and coeff1 is obtained from V1 and > V2 of first 4 rows lm(V2 ~ V1), and so on... > > > V1 V2 "intercept" "coeff" > 1 0.6931694 0.05797771 intercept1 coeff1 > 2 -1.4069786 0.23983307 intercept1 coeff1 > 3 -1.4901708 0.45079601 intercept1 coeff1 > 4 0.2215696 1.87888983 intercept1 coeff1 > 5 -0.5828106 0.90376622 intercept2 coeff2 > 6 -0.7607985 0.71419938 intercept2 coeff2 > 7 0.1273495 0.06199312 intercept2 coeff2 > 8 -0.5612245 1.02223971 intercept2 coeff2 > 9 -0.1439178 0.92135354 intercept3 coeff3 > 10 -1.1011662 0.02894731 intercept3 coeff3 > 11 -0.4098710 -0.01231322 intercept3 coeff3 > 12 1.1511811 -0.63923140 intercept3 coeff3 > > > Thanks in advance, > > Alfredo > > ______________________________________________ > [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. |
|
On Thu, Jul 7, 2011 at 5:24 PM, Dennis Murphy <[hidden email]> wrote:
> Hi: > > Here's another approach using the plyr package: > > library(plyr) > df <- data.frame(gp = factor(rep(1:3, each = 4)), x = rnorm(12), y = rnorm(12)) > mylst <- split(df, df$gp) > mycoefs <- ldply(mylst, function(d) coef(lm(y ~ x, data = d))) > names(mycoefs) <- c('gp', 'intercept', 'slope') > merge(df, mycoefs, by = 'gp', all.x = TRUE) > > One could write this more compactly as > > dfr <- merge(df, ldply(split(df, df$gp), function(d) coef(lm(y ~ x, data = d))), > by.x = 'gp', by.y = '.id', all.x = TRUE) Or even more compactly as dfr <- merge(df, ddply(df, "gp", function(d) coef(lm(y ~ x, data = d))), by.x = 'gp', by.y = '.id', all.x = TRUE) Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/ ______________________________________________ [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. |
| Powered by Nabble | Edit this page |
