Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:19:14+00:00 2026-06-02T11:19:14+00:00

The language I’m using is R, but you don’t necessarily need to know about

  • 0

The language I’m using is R, but you don’t necessarily need to know about R to answer the question.

Question:
I have a sequence that can be considered the ground truth, and another sequence that is a shifted version of the first, with some missing values. I’d like to know how to align the two.

setup

I have a sequence ground.truth that is basically a set of times:

ground.truth <- rep( seq(1,by=4,length.out=10), 5 ) +
                rep( seq(0,length.out=5,by=4*10+30), each=10 )

Think of ground.truth as times where I’m doing the following:

{take a sample every 4 seconds for 10 times, then wait 30 seconds} x 5

I have a second sequence observations, which is ground.truth shifted with 20% of the values missing:

nSamples <- length(ground.truth)
idx_to_keep <- sort(sample( 1:nSamples, .8*nSamples ))
theLag <- runif(1)*100
observations <- ground.truth[idx_to_keep] + theLag
nObs     <- length(observations)

If I plot these vectors this is what it looks like (remember, think of these as times):

enter image description here

What I’ve tried. I want to:

  • calculate the shift (theLag in my example above)
  • calculate a vector idx such that ground.truth[idx] == observations - theLag

First, assume we know theLag. Note that ground.truth[1] is not necessarily observations[1]-theLag. In fact, we have ground.truth[1] == observations[1+lagI]-theLag for some lagI.

To calculate this, I thought I’d use cross-correlation (ccf function).

However, whenever I do this I get a lag with a max. cross-correlation of 0, meaning ground.truth[1] == observations[1] - theLag. But I’ve tried this in examples where I’ve explicitly made sure that observations[1] - theLag is not ground.truth[1] (i.e. modify idx_to_keep to make sure it doesn’t have 1 in it).

The shift theLag shouldn’t affect the cross-correlation (isn’t ccf(x,y) == ccf(x,y-constant)?) so I was going to work it out later.

Perhaps I’m misunderstanding though, because observations doesn’t have as many values in it as ground.truth? Even in the simpler case where I set theLag==0, the cross correlation function still fails to identify the correct lag, which leads me to believe I’m thinking about this wrong.

Does anyone have a general methodology for me to go about this, or know of some R functions/packages that could help?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:19:16+00:00Added an answer on June 2, 2026 at 11:19 am

    For the lag, you can compute all the differences (distances) between your two sets of points:

    diffs <- outer(observations, ground.truth, '-')
    

    Your lag should be the value that appears length(observations) times:

    which(table(diffs) == length(observations))
    # 55.715382960625 
    #              86 
    

    Double check:

    theLag
    # [1] 55.71538
    

    The second part of your question is easy once you have found theLag:

    idx <- which(ground.truth %in% (observations - theLag))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Language Integrated Query. Now I know that the acronyms are. I have seen C#
My language of choice is Ruby, but I know because of twitter that Ruby
Language: Visual C++, MFC Environment: Visual Studio 2005 I have a dialog that requires
Every language that supports Object Orientation has a port of xUnit. What about for
My language is PHP, but the algorithm should be fairly universal. I have an
I am learning C language and I have some question as follows (sorry if
This is a language agnostic question, but I'm wandering what people prefer in terms
Language by choice is AspectJ but I am open for a generic answer.
Language Visual Basic 6. I want to know if there is an open source
Language: PHP I have a form which asks users for their educational details, course

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.