Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active December 28, 2015 07:09
Show Gist options
  • Save benmarwick/7462077 to your computer and use it in GitHub Desktop.
Save benmarwick/7462077 to your computer and use it in GitHub Desktop.
Interpolate age from depth using local fitting (aka loess)
###############################################################################
############ Interpolate age from depth using local fitting (aka loess) #######
# Assuming that we have a data frame called 'dates' that has a numeric variable called 'Depth.m'
# which is depth in meters and another called 'cal.median' which is an age determination in kya...
span <- 0.45 # this has a big influence on the shape of the curve, experiment with it!
cal.date.lo <- loess(cal.median ~ Depth.m, dates, span = span)
cal.date.pr <- predict(cal.date.lo, data.frame(Depth.m = seq(0, 5, 0.01)))
plot(cal.date.pr) # inspect to seee that it looks right
cal.date.pr <- data.frame(age = unname(cal.date.pr), depth = as.numeric(names(cal.date.pr)))
# Function to determine an age from a known depth
# Function to determine an age from a known depth
interpolator <- function(i){
cal.date.pr[cal.date.pr$depth == round(i*100),] # returns an estimated age in ky BP from loess interpolation
}
# Examples of use (replace the number in brackets with the depth you're
# interested in)
interpolator(0.4) # where 0.4 is the depth in meters
interpolator(1.20) # where 1.20 is the depth in meters
# and so on, note that you can't get the age of a depth below the lowest date
max(cal.date.pr$depth)/100 # depth of lowest date, in meters
interpolator( max(cal.date.pr$depth)/100 ) # age at max depth
# Note that interpolation is only a kind of estimation and not at all an exact # age for the given depth.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment