Interpolating missing values in timeframe ts()

Hi all,

I have a data frame with made up chicken population number that is recorded every 3 days for a total of 10 data points each month. (This is a made-up data I'm working on to practice before working with my actual data.)
Some months are missing 1 or 2 values and I would like to make it a timeseries to eventually calculate Sen's Slope. Can someone please help me put in a placeholder so that each month has 10 rows of data each? The 4th line of code I have for the interpolation does not work.
Thank you so much in advance!

This is what I have so far:

# making a date column
df$date <- as.Date(with(df, paste(year, month, day,sep="/")), "%Y/%m/%d")

# ordering by date to run the ts()
i <- order(df$date)

# making the timeseries
allts<-ts(data$chickens[i],start=c(1,1),frequency=10) 

# interpolating missing values 
allts<-na_seadec(allts,algorithm="interpolation",find_frequency=F,maxgap=Inf)

This is what my data looks like:

year month day chickens
2000 1 2 1
2000 1 5 2
2000 1 8 3
2000 1 11 4
2000 1 14 5
2000 1 17 6
2000 1 20 7
2000 1 23 8
2000 1 26 9
2000 2 3 2
2000 2 6 4
2000 2 9 6
2000 2 12 8
2000 2 15 10
2000 2 18 12
2000 2 21 14
2000 2 24 16
2000 2 27 18
2000 3 1 3
2000 3 4 6
2000 3 7 9
2000 3 10 12
2000 3 13 15
2000 3 16 18
2000 3 19 21
2000 3 22 24
2000 3 25 27
2000 3 28 30
2000 4 3 4
2000 4 6 8
2000 4 9 12
2000 4 12 16
2000 4 15 20
2000 4 18 24
2000 4 21 28
2000 4 24 32
2000 4 27 36
2000 5 3 5
2000 5 6 10
2000 5 9 15
2000 5 12 20
2000 5 15 25
2000 5 18 30
2000 5 21 35
2000 5 24 40
2000 5 27 45
2000 6 3 6
2000 6 6 12
2000 6 9 18
2000 6 12 24
2000 6 15 30
2000 6 18 36
2000 6 21 42
2000 6 24 48
2000 6 27 54
2000 7 3 7
2000 7 6 14
2000 7 9 21
2000 7 12 28
2000 7 15 35
2000 7 18 42
2000 7 21 49
2000 7 24 56
2000 7 27 63
2000 8 3 8
2000 8 6 16
2000 8 9 24
2000 8 12 32
2000 8 15 40
2000 8 18 48
2000 8 21 56
2000 8 24 64
2000 8 27 72
2000 9 3 9
2000 9 6 18
2000 9 9 27
2000 9 12 36
2000 9 15 45
2000 9 18 54
2000 9 21 63
2000 9 24 72
2000 9 27 81
2000 10 3 10
2000 10 6 20
2000 10 9 30
2000 10 12 40
2000 10 15 50
2000 10 18 60
2000 10 21 70
2000 10 24 80
2000 10 27 90
2000 11 3 11
2000 11 6 22
2000 11 9 33
2000 11 12 44
2000 11 15 55
2000 11 18 66
2000 11 21 77
2000 11 24 88
2000 11 27 99
2000 12 6 12
2000 12 9 24
2000 12 12 36
2000 12 18 48
2000 12 21 60
2000 12 24 72
2000 12 27 84

The zoo package in R allows you to work with time series that have unevenly spaced time intervals, and variable frequencies (no need to interpolate data). The {mblm} package does a Thiel-Sen regression on time-series-like data. Looking at your dummy data I assume that you want to do the regression for each month. Try this:

a <- "
year	month	day	chickens
2000	1	2	1
2000	1	5	2
2000	1	8	3
2000	1	11	4
2000	1	14	5
2000	1	17	6
2000	1	20	7
2000	1	23	8
2000	1	26	9
2000	2	3	2
2000	2	6	4
2000	2	9	6
2000	2	12	8
2000	2	15	10
2000	2	18	12
2000	2	21	14
2000	2	24	16
2000	2	27	18
2000	3	1	3
2000	3	4	6
2000	3	7	9
2000	3	10	12
2000	3	13	15
2000	3	16	18
2000	3	19	21
2000	3	22	24
2000	3	25	27
2000	3	28	30
2000	4	3	4
2000	4	6	8
2000	4	9	12
2000	4	12	16
2000	4	15	20
2000	4	18	24
2000	4	21	28
2000	4	24	32
2000	4	27	36
2000	5	3	5
2000	5	6	10
2000	5	9	15
2000	5	12	20
2000	5	15	25
2000	5	18	30
2000	5	21	35
2000	5	24	40
2000	5	27	45
2000	6	3	6
2000	6	6	12
2000	6	9	18
2000	6	12	24
2000	6	15	30
2000	6	18	36
2000	6	21	42
2000	6	24	48
2000	6	27	54
2000	7	3	7
2000	7	6	14
2000	7	9	21
2000	7	12	28
2000	7	15	35
2000	7	18	42
2000	7	21	49
2000	7	24	56
2000	7	27	63
2000	8	3	8
2000	8	6	16
2000	8	9	24
2000	8	12	32
2000	8	15	40
2000	8	18	48
2000	8	21	56
2000	8	24	64
2000	8	27	72
2000	9	3	9
2000	9	6	18
2000	9	9	27
2000	9	12	36
2000	9	15	45
2000	9	18	54
2000	9	21	63
2000	9	24	72
2000	9	27	81
2000	10	3	10
2000	10	6	20
2000	10	9	30
2000	10	12	40
2000	10	15	50
2000	10	18	60
2000	10	21	70
2000	10	24	80
2000	10	27	90
2000	11	3	11
2000	11	6	22
2000	11	9	33
2000	11	12	44
2000	11	15	55
2000	11	18	66
2000	11	21	77
2000	11	24	88
2000	11	27	99
2000	12	6	12
2000	12	9	24
2000	12	12	36
2000	12	18	48
2000	12	21	60
2000	12	24	72
2000	12	27	84"

df <- read.delim(text=a, header=TRUE)

# making a date column
df$date <- as.Date(with(df, paste(year, month, day,sep="/")), "%Y/%m/%d")

library(zoo)
library(mblm)

for(ii in 1:12) {
  tmp <- df[df$month==ii,]
  print(tmp)
  zz <- zoo(tmp$chickens, tmp$date)
  plot(zz, type="b")
  fit <- mblm(chickens ~ day, dataframe=tmp)
  print(summary(fit))
}

It's a for() loop, I know, but I'll leave someone else to get a nice dplyr or lapply() version working.

HTH

That worked a charm. Thank you so much DavoWW!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.