Skip to content

Instantly share code, notes, and snippets.

@MJacobs1985
Last active February 21, 2022 13:43
Show Gist options
  • Select an option

  • Save MJacobs1985/c4b1d60f2cf859429e657ab43db7bbd0 to your computer and use it in GitHub Desktop.

Select an option

Save MJacobs1985/c4b1d60f2cf859429e657ab43db7bbd0 to your computer and use it in GitHub Desktop.
inds <- seq(as.Date(min(day$date_interval)),
as.Date(max(day$date_interval)),
by = "day")
Eggs<-day%>%
filter(location_identification==1)%>%
mutate(egg_count = zoo::na.aggregate(egg_count))%>%
dplyr::select(egg_count)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
Feed<-day%>%
filter(location_identification==1)%>%
mutate(feed_provided = zoo::na.aggregate(feed_provided))%>%
dplyr::select(feed_provided)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
Water<-day%>%
filter(location_identification==1)%>%
mutate(water_provided = zoo::na.aggregate(water_provided))%>%
dplyr::select(water_provided)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
AnimalACT<-day%>%
filter(location_identification==1)%>%
mutate(animals_actual = zoo::na.aggregate(animals_actual))%>%
dplyr::select(animals_actual)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
TempMIN<-day%>%
filter(location_identification==1)%>%
mutate(t_min_out = zoo::na.aggregate(t_min_out))%>%
dplyr::select(t_min_out)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
TempMAX<-day%>%
filter(location_identification==1)%>%
mutate(t_max_out = zoo::na.aggregate(t_max_out))%>%
dplyr::select(t_max_out)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
TempACT<-day%>%
filter(location_identification==1)%>%
mutate(t_act_out = zoo::na.aggregate(t_act_out))%>%
dplyr::select(t_act_out)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
RH<-day%>%
filter(location_identification==1)%>%
mutate(rh_act_in = zoo::na.spline(rh_act_in))%>%
dplyr::select(t_act_out)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)
dfy <-ts.union(Eggs, Water, Feed)
dfexo<-ts.union(TempMIN, TempMAX, TempACT, AnimalACT)
autoplot(dfy, facets=TRUE) +
xlab("Date") + ylab("") +
ggtitle("Stals")+theme_bw()
ggAcf(dfy)+theme_bw()
ggPacf(dfy)+theme_bw()
tsdat_egg = dfy[,"Eggs"] %>% ts(., frequency=7)
tsdat_egg_stl = stl(tsdat_egg, s.window = "periodic")
autoplot(tsdat_egg_stl)+theme_bw()
tsdat_feed = dfy[,"Feed"] %>% ts(., frequency=7)
tsdat_feed_stl = stl(tsdat_feed, s.window = "periodic")
autoplot(tsdat_feed_stl)+theme_bw()
tsdat_avgweight = dfy[,"Water"] %>% ts(., frequency=7)
tsdat_avgweight_stl = stl(tsdat_avgweight, s.window = "periodic")
autoplot(tsdat_avgweight_stl)+theme_bw()
Eggs_ts_linear <- tslm(ts(tsdat_egg)~trend)
Eggs_ts_p5 <- tslm(ts(tsdat_egg)~trend + I(trend^2) + I(trend^3) + I(trend^4) + I(trend^5) ) # polynomial
Eggs_ts_ma5 <- ma(ts(tsdat_egg), order=5)
Eggs_ts_trends <- data.frame(cbind(Data=as.data.frame(tsdat_egg),
Linear_trend = fitted(Eggs_ts_linear),
Poly_trend = fitted(Eggs_ts_p5),
Moving_avg5 = Eggs_ts_ma5))
Eggs_ts_trends <- tibble::rownames_to_column(Eggs_ts_trends, "VALUE")
ggplot(Eggs_ts_trends,aes(x=as.numeric(VALUE))) +
geom_line(aes(y=x , color="original"),size=1) +
geom_line(aes(y=Linear_trend , color="linear"),size=1) +
geom_line(aes(y=Poly_trend, color = "O(5) poly"), size=1) +
geom_line(aes(y=Moving_avg5, color ="ma21"), size=1) +
scale_color_manual(values = c('original'= 'grey',
'linear' = 'darkblue',
'O(5) poly' = 'red',
'ma21'= 'orange')) +
labs(color = 'Trend fit') + ylab("Eggs") +
ggtitle("Different trend fits for Eggs") +theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment