how to allign data

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

how to allign data

PIKAL Petr
Dear all

Before I start to reinvent wheel I would like to ask you if you have some easy solution for aligning data

I have something like this
x<-1:100
set.seed(42)
y1<-c(runif(20)+1, 1.2*x[1:80]+runif(80))
y2<-c(runif(40)+1, 1.2*x[1:60]+runif(60))

plot(x,y1)
points(x,y2, col=2)

with y increase starting at various x.

I would like to allign data so that the increase starts at the same x point, something like

plot(x,y1)
points(x[-(1:20)]-20,y2[-(1:20)], col=2)

I consider using strucchange or segmented packages to find break(s) and "shift" x values according to this break. But maybe somebody already did similar task (aligning several vectors according to some common breakpoint) and could offer better or simpler solution.

Best regards.
Petr
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to allign data

Jim Lemon-4
Hi Petr,
I recently had to align the minima of deceleration events to form an
aggregate "braking profile" for different locations. It seems as
though you are looking for something like:

find_increase<-function(x,surround=10) {
 inc_index<-which.max(diff(x))
 indices<-(inc_index-surround):(inc_index+surround)
 nneg<-sum(indices < 1)
 # pad both ends with NA if needed
 newx<-x[1:max(indices)]
 if(nneg > 0) newx<-c(rep(NA,nneg),newx)
 return(newx)
}

Jim

On Thu, Aug 2, 2018 at 10:23 PM, PIKAL Petr <[hidden email]> wrote:

> Dear all
>
> Before I start to reinvent wheel I would like to ask you if you have some easy solution for aligning data
>
> I have something like this
> x<-1:100
> set.seed(42)
> y1<-c(runif(20)+1, 1.2*x[1:80]+runif(80))
> y2<-c(runif(40)+1, 1.2*x[1:60]+runif(60))
>
> plot(x,y1)
> points(x,y2, col=2)
>
> with y increase starting at various x.
>
> I would like to allign data so that the increase starts at the same x point, something like
>
> plot(x,y1)
> points(x[-(1:20)]-20,y2[-(1:20)], col=2)
>
> I consider using strucchange or segmented packages to find break(s) and "shift" x values according to this break. But maybe somebody already did similar task (aligning several vectors according to some common breakpoint) and could offer better or simpler solution.
>
> Best regards.
> Petr
> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to allign data

PIKAL Petr
Hi Jim

Thanks for your function, however I either do not understand its purpose or I did not explained my aim correctly.

with segmented package I could find x value for break in slope
fit<-lm(y1~x)
segmented(fit, seg.Z=~x)
Estimated Break-Point(s):
psi1.x
 20.77
fit<-lm(y2~x)
segmented(fit, seg.Z=~x)
Estimated Break-Point(s):
psi1.x
 40.99

After that I need to allign both y1 and y2 to common x, for which I could use the difference in estimated beakpoints for each y, which is
> round(40.99-20.77)
[1] 20

and use this difference for aligning data

plot(x,y1)
points(x[-(1:(psi20)]-20,y2[-(1:20)], col=2)

But this seems to me rather complicated so I just asked if there is some shorter and less tedious option.

Actually I cannot decipher how your function could help me to plot y1 and y2 and starting to increase at the same point.

Cheers
Petr

> -----Original Message-----
> From: Jim Lemon <[hidden email]>
> Sent: Friday, August 3, 2018 12:32 AM
> To: PIKAL Petr <[hidden email]>
> Cc: r-help mailing list <[hidden email]>
> Subject: Re: [R] how to allign data
>
> Hi Petr,
> I recently had to align the minima of deceleration events to form an
> aggregate "braking profile" for different locations. It seems as
> though you are looking for something like:
>
> find_increase<-function(x,surround=10) {
>  inc_index<-which.max(diff(x))
>  indices<-(inc_index-surround):(inc_index+surround)
>  nneg<-sum(indices < 1)
>  # pad both ends with NA if needed
>  newx<-x[1:max(indices)]
>  if(nneg > 0) newx<-c(rep(NA,nneg),newx)
>  return(newx)
> }
>
> Jim
>
> On Thu, Aug 2, 2018 at 10:23 PM, PIKAL Petr <[hidden email]> wrote:
> > Dear all
> >
> > Before I start to reinvent wheel I would like to ask you if you have some easy
> solution for aligning data
> >
> > I have something like this
> > x<-1:100
> > set.seed(42)
> > y1<-c(runif(20)+1, 1.2*x[1:80]+runif(80))
> > y2<-c(runif(40)+1, 1.2*x[1:60]+runif(60))
> >
> > plot(x,y1)
> > points(x,y2, col=2)
> >
> > with y increase starting at various x.
> >
> > I would like to allign data so that the increase starts at the same x point,
> something like
> >
> > plot(x,y1)
> > points(x[-(1:20)]-20,y2[-(1:20)], col=2)
> >
> > I consider using strucchange or segmented packages to find break(s) and
> "shift" x values according to this break. But maybe somebody already did
> similar task (aligning several vectors according to some common breakpoint)
> and could offer better or simpler solution.
> >
> > Best regards.
> > Petr
> > Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních
> partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-
> ochrany-osobnich-udaju/ | Information about processing and protection of
> business partner’s personal data are available on website:
> https://www.precheza.cz/en/personal-data-protection-principles/
> > Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou
> důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení
> odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any
> documents attached to it may be confidential and are subject to the legally
> binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
> >
> > ______________________________________________
> > [hidden email] 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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

______________________________________________
[hidden email] 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.