[R] using ddply with segmented regression
arun
smartpink111 at yahoo.com
Sat Oct 12 08:41:33 CEST 2013
Hi,
Try:
segmentf_df <- function(df) {
out.lm<-lm(deltaWgt~Cycle, data=df)
segmented(out.lm,seg.Z=~Cycle, psi=(Cycle=NA),control=seg.control(stop.if.error=FALSE,n.boot=0))
}
library(plyr)
library(segmented)
dlply(df,.(Lot.Run),segmentf_df)
$`J062431-1`
Call: segmented.lm(obj = out.lm, seg.Z = ~Cycle, psi = (Cycle = NA),
control = seg.control(stop.if.error = FALSE, n.boot = 0))
Meaningful coefficients of the linear terms:
(Intercept) Cycle U1.Cycle U2.Cycle
38.480 1.130 -2.760 1.497
Estimated Break-Point(s) psi1.Cycle psi2.Cycle : 3.732 5.056
$`J062431-2`
Call: segmented.lm(obj = out.lm, seg.Z = ~Cycle, psi = (Cycle = NA),
control = seg.control(stop.if.error = FALSE, n.boot = 0))
Meaningful coefficients of the linear terms:
(Intercept) Cycle U1.Cycle U2.Cycle
48.4300 -3.2500 3.0905 -0.6555
Estimated Break-Point(s) psi1.Cycle psi2.Cycle : 2.12 22.15
attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
Lot.Run
1 J062431-1
2 J062431-2
#or
dlply(df,.(Lot.Run),function(x) segmentf_df(x))
#or
lapply(split(df,df$Lot.Run,drop=TRUE),function(x) segmentf_df(x))
A.K.
On Friday, October 11, 2013 11:16 PM, "Prew, Paul" <Paul.Prew at ecolab.com> wrote:
Hello,
I’m unsuccessfully trying to apply piecewise linear regression over each of 22 groups. The data structure of the reproducible toy dataset is below. I’m using the ‘segmented’ package, it worked fine with a data set that containing only one group (“Lot.Run”).
$ Cycle : int 1 2 3 4 5 6 7 8 9 10 ...
$ Lot.Run : Factor w/ 22 levels "J062431-1","J062431-2",..: 1 1 1 1 1 1 1 1 1 1 ...
$ deltaWgt: num 38.7 42.6 41 42.3 40.6 ...
I am new to ‘segmented’, and also new to ‘plyr’, which is how I’m trying to apply this segmented regression to the 22 Lot.Run groups. Within a Lot.Run, the piecewise linear regressions are deltaWgt vs. Cycle.
##### define the linear regression #####
out.lm<-lm(deltaWgt~Cycle, data=Test50.df)
##### define the function called by dlply #####
##### find cutpoints via bootstrapping, fit the piecewise regressions #####
segmentf_df <- function(df) {
segmented(out.lm,seg.Z=~Cycle, psi=(Cycle=NA),control=seg.control(stop.if.error=FALSE,n.boot=0)), data = df)
}
at this point, there’s an error message
23] ERROR: <text>
##### repeat for each Lot.Run group #####
dlply(Test50.df, .(Lot.Run), segmentf_df)
at this point, there’s an error message
[28] ERROR:
object 'segmentf_df' not found
Any suggestions?
Thanks, Paul
> dput(Test50.df)
structure(list(Cycle = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L), Lot.Run = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("J062431-1",
"J062431-2", "J062431-3", "J062432-1", "J062432-2", "J062433-1",
"J062433-2", "J062433-3", "Lot 1-1", "Lot 1-2", "Lot 2-1", "Lot 2-2",
"Lot 2-3", "Lot 3-1", "Lot 3-2", "Lot 3-3", "P041231-1", "P041231-2",
"P041531-1", "P041531-2", "P041531-3", "P041531-4"), class = "factor"),
deltaWgt = c(38.69, 42.58, 40.95, 42.26, 40.63, 41.61, 36.73,
41.28, 39.98, 40.63, 39.66, 39.98, 40.95, 38.36, 39.01, 39,
38.03, 39.66, 37.7, 39.66, 40.63, 38.03, 37.71, 36.73, 37.7,
45.18, 41.93, 42.59, 39.98, 40.95, 42.91, 38.03, 40.96, 39,
41.61, 39.33, 43.88, 39.98, 38.68, 38.68, 36.08, 39.99, 38.35,
40.31, 40.63, 38.68, 37.05, 38.36, 35.43, 36.73)), .Names = c("Cycle",
"Lot.Run", "deltaWgt"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 207L, 208L, 209L, 210L, 211L, 212L,
213L, 214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L,
224L, 225L, 226L, 227L, 228L, 229L, 230L, 231L), class = "data.frame")
Paul Prew ▪ Statistician
651-795-5942 ▪ fax 651-204-7504
Ecolab Research Center ▪ Mail Stop ESC-F4412-A
655 Lone Oak Drive ▪ Eagan, MN 55121-1560
CONFIDENTIALITY NOTICE: This e-mail communication and any attachments may contain proprietary and privileged information for the use of the designated recipients named above. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org 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.
More information about the R-help
mailing list