Этот ответ основан на данных imgur.com / a / L22T4BN
library(tidyverse)
# I've reproduced a subset of your data
df <- data.frame(Date = c('21/05/2019','21/05/2019','21/05/2019','21/05/2019','21/05/2019','21/05/2019','21/05/2019','21/05/2019','21/05/2019'),
Time24 = c('15:45:22',
'15:18:11',
'15:22:10',
'15:18:38',
'15:40:50',
'15:51:42',
'15:38:29',
'15:20:20',
'15:41:34'
),
MPM25 = c(46, 34, 57, 51, 31, 32,46,33,31))
glimpse(df)
# Variables: 4
# $ Date <fct> 21/05/2019, 21/05/2019, 21/05/2019, 21/05/2019, 21/05/2019, 21/05/2019
# $ Time24 <fct> 15:18:11, 15:18:38, 15:22:10, 15:40:50, 15:45:22, 15:51:42
# $ MPM25 <dbl> 34, 51, 57, 31, 46, 32
# $ datetime <dttm> 2019-05-21 15:18:11, 2019-05-21 15:18:38, 2019-05-21 15:22:10, 2019-0
# Note that the Date and Time24 are factors <fct>
# We can use these values to create a datetime object
# Also note the dates/time are out of order because they some from a random sample
df <- df %>%
mutate(datetime = str_c(as.character(Date), as.character(Time24), sep = ' ')) %>% # join date and time
mutate(datetime = lubridate::dmy_hms(datetime)) %>% # convert to datetime object
mutate(num_datetime = as.numeric(datetime)) %>% # numerical version of datetime required for loess fitting
arrange(datetime) # put times in order
# Take care with the time zone. The function dmy_hms defaults to UTC.
# You may need to use the timezone for your area e.g. for me it would be tz = 'australia/melbourne'
# we can then plot
df %>%
ggplot(aes(x = datetime, y = MPM25)) +
geom_point() +
geom_smooth(span = 0.9) # loess smooth
# fitting a loess
m_loess <- loess(MPM25 ~ num_datetime, data = df, span = 0.9) #fit a loess model
# Create predictions
date_seq <- seq(from = 1558451891, # 100 points from the first to the late datetime
to = 1558453902,
length.out = 100)
m_loess_pred <- predict(m_loess,
newdata = data.frame(num_datetime = date_seq))
# To plot the dates they need to be in POSIXct format
date_seq <- as.POSIXct(date_seq, tz = 'UTC', origin = "1970-01-01")
# Create a dataframe with times and predictions
df_predict <- data.frame(datetime = date_seq, pred = m_loess_pred)
# Plot to show that geom_smooth and the loess predictions are the same
df %>%
ggplot(aes(x = datetime, y = MPM25)) +
geom_point() +
geom_smooth(span = 0.9, se = FALSE) +
geom_point(data = df_predict, aes(x = datetime, y = pred) , colour = 'orange')