bgf_interpolation: Data interpolation algorithm

View source: R/data_interpolation.R

bgf_interpolationR Documentation

Data interpolation algorithm

Description

A function that allows to interpolate missing values in a numeric vector of a data.frame. It requires a second column in the data.frame representing time, as well as a third column representing grouping information.

Usage

bgf_interpolation(df, x, t, group, end = TRUE, sub_zero = NULL, offset = 1)

Arguments

df

a data.frame

x

an integer specifying the position of the vector with missing values that should be interpolated

t

an integer specifying the position of the vector with time data over which to interpolate

group

an integer specifying the position of a grouping vector within the input data

end

logic; default is TRUE. Should interpolation end if the last valid 'x' value does not match the final 't' value?

sub_zero

default is NULL. Can be a numeric which will be used to replace all (interpolated) values in 'x' that are below '0'.

offset

an integer; default is 1. Can be used to specify the offset (distance between valid 'x' values) that is used during terminal data extrapolation (argument 'end = FALSE'). With the default setting ('offset = 1') the last valid 'x' value and 1 value before that are selected.

Details

The function bgf_interpolation takes a data.frame as input and returns a modified version of it as output. In particular, it interpolates missing values in a numeric vector (argument 'x') based on a second vector (argument 't') without missing values.

It is possible to stop the interpolation (argument 'end'), if no valid value in 'x' occurs after a missing value, or to use linear extrapolation to fill terminal NA's. The slope of the extrapolation is based on the last valid 'x' value and the n^{th} valid 'x' value before (argument 'offset').

Furthermore, if linear extrapolation leads to e.g. negative gas concentrations or volumes, it is possible to replace these wrong values with any replacement (argument 'sub_zero')

Interpolation and extrapolation are done specifically for groups within the data (argument 'group')

Value

a data.frame

Examples

# create an example data.frame
gap_data=data.frame(
              time=as.numeric(rep(c(1:30),3)),
              value=c(0,rep(NA,3),7,14,26,44,72,rep(NA,8),314,350,
                      377,rep(NA,5),471,rep(NA,4),0,rep(NA,7),100,
                      127,163,rep(NA,4),217,rep(NA,5),267,rep(NA,4),
                      0,rep(NA,3),0,rep(NA,3),5,rep(NA,3),12,rep(NA,3),
                      32,rep(NA,3),35,rep(NA,3),33,rep(NA,3),28,rep(NA,3),
                      5,NA),
              cat=c(rep("exGas",30),rep("exGas_2",30),rep("cBioG",30)))

# interpolate and extrapolate missing values, replace values below '0' with '0'
closed_data<- bgf_interpolation(gap_data,2,1,3,end = FALSE,sub_zero = 0)

# inspect the differences
plot(value ~ time,data=gap_data,col=as.factor(cat))
legend(legend = levels(as.factor(gap_data$cat)),col=c(1,2,3),x = "topleft",pch=15)

plot(value ~ time,data=closed_data,col=as.factor(cat))
legend(legend = levels(as.factor(closed_data$cat)),col=c(1,2,3),x = "topleft",pch=15)


bgfanalyzer documentation built on Sept. 26, 2026, 5:07 p.m.