How to use ChatGPT for Time Series in R

[This article was first published on business-science.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Writing R code is a slow process especially when you are first learning Time Series Analysis. What if you could speed it up? You can and this is how. In this free R-tip, I share a real case study where I made the working R code for my time series analysis and saved over 3 hours of work using ChatGPT.

Table of Contents

Today I share how to automate 80% of your Time Series Analysis code with ChatGPT. Here’s what you’re learning today:

  • ChatGPT Prompts: The mistakes you’ll make and how to get ChatGPT to write your time series analysis code correctly.
  • 2-Minute Case Study: How I made a time series analysis with ChatGPT.
  • Bonus: Sneak Peek At My NEW Shiny App that extends this ChatGPT tutorial 👇

ChatGPT Made This Shiny App

Bonus: A shiny app that extends this tutorial


SPECIAL ANNOUNCEMENT: ChatGPT for Data Scientists Workshop on July 26th

Inside the workshop I’ll share how I built a Machine Learning Powered Production Shiny App with ChatGPT (extends this data analysis to an insane production app):

ChatGPT for Data Scientists

What: ChatGPT for Data Scientists

When: Wednesday July 26th, 2pm EST

How It Will Help You: Whether you are new to data science or are an expert, ChatGPT is changing the game. There’s a ton of hype. But how can ChatGPT actually help you become a better data scientist and help you stand out in your career? I’ll show you inside my free chatgpt for data scientists workshop.

Price: Does Free sound good?

How To Join: 👉 Register Here


R-Tips Weekly

This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks. Pretty cool, right?

Here are the links to get set up. 👇

My Previous ChatGPT R Tutorials

So listen, time series is a tough problem to solve. And there’s a learning curve with ChatGPT. To help I’ve done 2 R-Tips on ChatGPT. These are good to start with if you’ve never used ChatGPT before (or want starter tutorials that are great for beginners):

  1. I showed you how ChatGPT I made a full R Shiny App in under 10-minutes. That was insane!

  2. I demonstrated how important ChatGPT prompts are giving you a full ChatGPT prompt guide and walkthrough

But now I want to take it a step up and demonstrate using ChatGPT for a more difficult problem…

Using ChatGPT for Time Series in R

I’m going to be fully transparent here. ChatGPT is NOT 100% with Time Series yet. It got me 80% of the way there. BUT the last mile, I actually had to know time series.

I also tried Bard. Bard was even worse than ChatGPT’s GPT-4 model.

To help, I want to give you some pointers.

ChatGPT Prompt Guide for Time Series

The mistake I made when I first started using ChatGPT was not being specific enough in directing chatgpt what R code I want it to write for me. I’ve since learned from this.

Here’s a good starter chatgpt prompt for my time series problem:

ChatGPT Prompt for time series

Looks pretty straightforward, but it’s actually quite complex. Let’s break it down.

ChatGPT Prompt Guide for Time Series

The key is specifying:

  1. Which R Libraries to Use: I specified to forecast using the modeltime library. Full Disclosure: I’m the creator. But, it IS awesome for time series.
  2. What Time Series Goal: For my time series project it’s to forecast. Other time series problems might involve exploratory plotting, anomaly detection, or decomposition.
  3. What Inputs to the Time Series Problem: For this time series project, I want to forecast initially the next 30 days (the forecast horizon) and it’s a daily time series data set with 2 columns: date and value.

And ChatGPT generates the code for me:

ChatGPT Code OSMdata Example

Steal my code (it’s legal).

Does the code work?

A big problem that data scientists are facing is that chatgpt code isn’t working out of the box.

So let’s test the code. I ran it, and it turns out…

ChatGPT has Hallucinations

Does the ChatGPT code work?

It didn’t run. What happened is so common people have termed it “ChatGPT Hallucinations”.

Yep, chatgpt makes stuff up. And this is why it’s important to know how to code. Case in point: modeltime.arima is not an R package. I have no clue where it got this.

The reality: You need to know how to code and do time series

Fortunately, I’ve been doing this a while. And I just had to debug the code. So here’s what my process looks like:

  1. ChatGPT wrote the code (15 Seconds)
  2. I debugged the code (10 minutes)

The net time was a little over 10 minutes for me to get this code working.

ChatGPT Corrected Code

Get My Working Code Here (It’s in the 064_chatgpt_time_series folder).

And the modified ChatGPT code now produces a test forecast using an ARIMA model. Now, let’s set aside that the forecast isn’t actually that good (I’ll improve this with a different model). But I have something usable.

Test Forecast from an ARIMA Model

Total Time Savings: 30 minutes.

I don’t have the completed solution yet

I just have a test forecast at this point. I had to go back and request chatpgt to refit the ARIMA model on the full dataset and forecast the next 30 days.

Refit and Forecast Next 30 Days

What it gave me again had some hallucinations that I had to fix. No big deal. This took maybe 5 minutes.

ChatGPT Corrections

Steal my code (it’s legal).

And, the modified chatgpt code now produces a 30-day forecast into the future, which is pretty cool.

The second time series forecast

So at this point I’ve spent 15 minutes making this forecast. Something that would have taken about an hour to write 100 lines of code.

Total Time Savings: 45 minutes.

But here’s where the time saving stacks up.

BONUS: I asked ChatGPT to make a Shiny App to Forecast with Modeltime

Bonus Shiny App

I asked ChatGPT to make a Shiny App using my corrected code. Making a shiny app is a task that would have taken me 3+ hours.

Asking ChatGPT to Make a Shiny App

I spent 15 minutes correcting the code. And I was able to produce this shiny app that uses a better XGBoost Model to forecast this time series.

Total Time Savings: 3 hours 30 minutes

Want my Bonus Shiny Time Series App?

Want all the code I just showed you PLUS my bonus Shiny Time Series App? It’s my free gift to you: You can have my shiny app library.

Bonus Shiny App

Steal my shiny app from this tutorial.

The code for the bonus shiny time series app is inside of R-Tip 064_chatgpt_time_series. Enjoy.

Oh, I forgot to mention something. There’s a…

Big Problem: You still need to learn how to do Time Series

If you learn one thing from this tutorial, it better be that ChatGPT isn’t the full solution. It’s a productivity enhancement. As you just saw, I completed 4 hours of work in under 30 minutes.

But, what’s going to happen when you try this on your own?

Don’t know? Allow me to answer: If you can’t do data science, time series, OR code… Well, then you aren’t going to get very far.

Would you like help? Here’s how I can help you become a data scientist and land a 6-figure job you love and learn how to use ChatGPT better (all for free).

SPECIAL ANNOUNCEMENT: ChatGPT for Data Scientists Workshop on July 26th

Inside the workshop I’ll share how I built a Machine Learning Powered Production Shiny App with ChatGPT (extends this data analysis to an insane production app):

ChatGPT for Data Scientists

What: ChatGPT for Data Scientists

When: Wednesday July 26th, 2pm EST

How It Will Help You: Whether you are new to data science or are an expert, ChatGPT is changing the game. There’s a ton of hype. But how can ChatGPT actually help you become a better data scientist and help you stand out in your career? I’ll show you inside my free chatgpt for data scientists workshop.

Price: Does Free sound good?

How To Join: 👉 Register Here

To leave a comment for the author, please follow the link and comment on their blog: business-science.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)