2
$\begingroup$

I have a time series dataset that has several variables for a state/province for fixed periods of time. That is for state A, there are samples from April 2017 to July 2019. Of course, I thought adding precipitation and temperature variables would be a great idea. I tried finding some relevant external data but most of it is abstract and spread out. How would one simulate dynamic data in Python with varying means, highs and lows for say six months on a daily basis, taking into account average temperatures/precipitation for each month?

So if I have the mean temperatures (C) as below for state A:

year    Jan   Feb   Mar   Apr   May   Jun
2017    5.5   6.0   12.0  15.0  20.0  25.0

I would like data to be simulated as below without really doing for each month since that would make the whole task very tedious:

Duration     Temp
2017-01-01   5.0
2017-01-02   5.1
2017-01-03   4.9
.
2017-03-01   7.8
2017-03-02   9.0
2017-03-03   9.5
.
2017-06-30   26.7

Are there ways to achieve this in Python (or R)?

$\endgroup$

2 Answers 2

4
$\begingroup$

What about creating a Pandas DataFrame and adding a new column such as "Temp_simulated" and simulate the temperature?

enter image description here

$\endgroup$
2
$\begingroup$

I'm not sure it is the best way to do it in r, but you can create a vector simulated temperature for each days of the year by using your reference vector with few temperature by doing the following:

1) You set a dataframe containing few temperatures as references for each month (here, I used lubridate package to manipulate dates):

library(lubridate)
Date = seq(ymd('2019-01-01'),ymd('2020-01-01'),by='months')
Temp_ref = c(5.5,6.0,12,15,20, 25, 25, 20,15,12,6,5.5,5.5)
df_ref <- data.frame(Date,Temp_ref)

         Date Temp_ref
1  2019-01-01      5.5
2  2019-02-01      6.0
3  2019-03-01     12.0
4  2019-04-01     15.0
5  2019-05-01     20.0
6  2019-06-01     25.0
7  2019-07-01     25.0
8  2019-08-01     20.0
9  2019-09-01     15.0
10 2019-10-01     12.0
11 2019-11-01      6.0
12 2019-12-01      5.5
13 2020-01-01      5.5

If you plot it using ggplot2 and passed the function geom_smooth, you can have a simulation of this data every day:

library(ggplot2)
ggplot(df_ref, aes(x = Date, y = Temp_ref))+
  geom_point()+
  geom_smooth()

enter image description here

2) We can recreate this simulation by using loess function:

model <- loess(Temp_ref~as.numeric(Date), data = df_ref)

3) Now, we are using predict function to use this model to define temperature over the year for each day:

library(lubridate)
date <- seq(ymd('2019-01-01'),ymd('2019-12-30'),by='days')
df <- data.frame(date)
df$yfitted <- predict(model2, newdata = as.numeric(df$date))

4) We can confirm our fit by plotting it using ggplot2:

ggplot(df, aes(x = date, y = yfitted))+
  geom_point(size = 1)+
  geom_point(inherit.aes = FALSE, data = df_ref, aes(x = Date, y = Temp_ref), color = "red")

enter image description here

I'm not sure it is the perfect way to do it, but I think it gives you a good approximation of the evolution of the temperature over the year based on your reference vector.

Hope it helps you to figure it out the solution to your question.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.