Skip to content

Commit

Permalink
Added examples and CRAN requirements.
Browse files Browse the repository at this point in the history
  • Loading branch information
MMenchero committed Feb 1, 2024
1 parent ca673cb commit 5812e42
Show file tree
Hide file tree
Showing 29 changed files with 178 additions and 27 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
^\.github$
^README\.Rmd$
^codecov\.yml$
^cran-comments\.md$
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Package: nixtlar
Title: R SDK For Nixtla's TimeGPT
Title: R SDK for Nixtla's TimeGPT
Version: 1.0.0
Authors@R:
person("Mariana", "Menchero", , "[email protected]", role = c("aut", "cre"))
Authors@R: c(
person("Mariana", "Menchero", email = "[email protected]", role = c("aut", "cre"), comment = "First author and maintainer"),
person("Nixtla", role = "cph", comment = "Copyright held by Nixtla")
)
Description: An SDK to interface with Nixtla's TimeGPT from R.
It provides functions for setting and validating tokens and for generating forecasts with TimeGPT via API calls.
Compatible with the tsibble package and base R.
Expand Down
6 changes: 6 additions & 0 deletions R/date_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#' @return A list with the inferred frequency and df with the new index.
#' @export
#'
#' @examples
#' df <- AirPassengers
#' tsbl <- tsibble::as_tsibble(df)
#' names(tsbl) <- c("ds", "y")
#' date_conversion(tsbl)
#'
date_conversion <- function(df){
cls <- class(df$ds)[1]

Expand Down
4 changes: 4 additions & 0 deletions R/get_token.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#' @return If available, the TIMEGTP_TOKEN
#' @export
#' @keywords internal
#' @examples
#' \dontrun{
#' .get_token() # Assumes token was previously set successfully
#' }
#'
.get_token <- function(){

Expand Down
4 changes: 4 additions & 0 deletions R/infer_frequency.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#' @return The inferred frequency.
#' @export
#'
#' @examples
#' df <- nixtlar::electricity
#' infer_frequency(df)
#'
infer_frequency <- function(df){
freq <- NULL
dates <- sort(unique(df$ds))
Expand Down
5 changes: 5 additions & 0 deletions R/nixtla_set_token.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#' @return A message indicating the token has been set in the global environment.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' }
#'
nixtla_set_token <- function(token) {
options("TIMEGPT_TOKEN"=token)
message("Token has been set for the current session.")
Expand Down
6 changes: 6 additions & 0 deletions R/nixtla_validate_token.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#' @return A message indicating whether the token is valid.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' nixtlar::nixtla_validate_token
#' }
#'
nixtla_validate_token <- function(){

token <- .get_token()
Expand Down
7 changes: 7 additions & 0 deletions R/timegpt_anomaly_detection.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
#' @return A tsibble or a data frame with the anomalies detected in the historical period.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_anomaly_detection(df, id_col="unique_id")
#' }
#'
timegpt_anomaly_detection <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_col="y", level=c(99), clean_ex_first=TRUE, model="timegpt-1"){

# Prepare data ----
Expand Down
7 changes: 7 additions & 0 deletions R/timegpt_cross_validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
#' @return A tsibble or a data frame with TimeGPT's cross validation.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_cross_validation(df, h = 8, id_col = "unique_id", n_windows = 5)
#' }
#'
timegpt_cross_validation <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", target_col="y", X_df=NULL, level=NULL, n_windows=1, step_size=NULL, finetune_steps=0, clean_ex_first=TRUE, model="timegpt-1"){

# Prepare data ----
Expand Down
4 changes: 4 additions & 0 deletions R/timegpt_data_prep.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#' @export
#' @keywords internal
#'
#' @examples
#' df <- nixtlar::electricity
#' data <- .timegpt_data_prep(df, freq="H")
#'
.timegpt_data_prep <- function(df, freq, id_col, time_col, target_col){

if(!tsibble::is_tsibble(df) & !is.data.frame(df)){
Expand Down
7 changes: 7 additions & 0 deletions R/timegpt_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
#' @return TimeGPT's forecast.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_forecast(df, h=8, id_col="unique_id", level=c(80,95))
#' }
#'
timegpt_forecast <- function(df, h=8, freq=NULL, id_col=NULL, time_col="ds", target_col="y", X_df=NULL, level=NULL, finetune_steps=0, clean_ex_first=TRUE, add_history=FALSE, model="timegpt-1"){

# Prepare data ----
Expand Down
7 changes: 7 additions & 0 deletions R/timegpt_historic.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
#' @return TimeGPT's forecast for the in-sample period.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_historic(df, id_col="unique_id", level=c(80,95))
#' }
#'
timegpt_historic <- function(df, freq=NULL, id_col=NULL, time_col="ds", target_col="y", level=NULL, finetune_steps=0, clean_ex_first=TRUE){

# Prepare data ----
Expand Down
8 changes: 8 additions & 0 deletions R/timegpt_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
#' @return Plot with the forecast and insample values
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_token("YOUR_TOKEN")
#' df <- nixtlar::electricity
#' fcst <- nixtlar::timegpt_forecast(df, h=8, id_col="unique_id", level=c(80,95))
#' nixtlar::timegpt_plot(df, fcst, h=8, id_col="unique_id")
#' }
#'
timegpt_plot <- function(df, fcst=NULL, h=NULL, id_col=NULL, time_col="ds", target_col="y", unique_ids = NULL, max_insample_length=NULL, plot_anomalies=FALSE){

if(!tsibble::is_tsibble(df) & !is.data.frame(df)){
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ knitr::opts_chunk$set(

<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/nixtlar)](https://CRAN.R-project.org/package=nixtlar)
[![R-CMD-check](https://github.com/MMenchero/nixtlar/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/MMenchero/nixtlar/actions/workflows/R-CMD-check.yaml)
[![R-CMD-check](https://github.com/Nixtla/nixtlar/actions/workflows/R-CMD-check.yaml)](https://github.com/Nixtla/nixtlar/actions/workflows/R-CMD-check.yaml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue)](https://www.apache.org/licenses/LICENSE-2.0)
<!-- badges: end -->

Expand Down
36 changes: 16 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

[![CRAN
status](https://www.r-pkg.org/badges/version/nixtlar)](https://CRAN.R-project.org/package=nixtlar)
[![R-CMD-check](https://github.com/MMenchero/nixtlar/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/MMenchero/nixtlar/actions/workflows/R-CMD-check.yaml)
[![R-CMD-check](https://github.com/Nixtla/nixtlar/actions/workflows/R-CMD-check.yaml)](https://github.com/Nixtla/nixtlar/actions/workflows/R-CMD-check.yaml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue)](https://www.apache.org/licenses/LICENSE-2.0)
<!-- badges: end -->

The `nixtlar` package provides R users with a SDK for [Nixtla’s
TimeGPT](https://docs.nixtla.io/).

## Your gateway to TimeGPT
### Your gateway to TimeGPT

TimeGPT is a cutting-edge generative pre-trained transformer model that
has been trained on the largest collection of publicly available time
Expand All @@ -24,7 +24,7 @@ democratizes forecasting, enabling rapid and accurate predictions, even
for datasets not seen during training. Learn more about TimeGPT
[here](https://arxiv.org/abs/2310.03589).

## Installation
### Installation

You can install the development version of `nixtlar` from
[GitHub](https://github.com/) with:
Expand All @@ -34,38 +34,34 @@ You can install the development version of `nixtlar` from
devtools::install_github("Nixtla/nixtlar")
```

## Example
### Example

Get started with TimeGPT now.

``` r
library(nixtlar)

# Load sample dataset
df <- nixtlar::electricity # can also be a tsibble!
df <- nixtlar::electricity # this can also be a tsibble!

# Set TIMEGPT Token
nixtlar::nixtla_set_token("TIMEGPT TOKEN")
#> Token has been set for the current session.

# Forecast the next 8 horizons using TimeGPT
# Forecast the next 8 steps using TimeGPT
fcst <- nixtlar::timegpt_forecast(df, h = 8, id_col = "unique_id", level = c(80,95))
#> Frequency chosen: H

# Plot TimeGPT forecast
nixtlar::timegpt_plot(df, fcst, h = 8, id_col = "unique_id", max_insample_length = 100)
#> Frequency chosen: H
```

<img src="man/figures/README-unnamed-chunk-2-1.png" width="100%" />
Using TimeGPT through `nixtlar` requires a token provided by Nixtla. To
see how to obtain it and learn about different ways to set it up, please
read the [Get
Started](https://nixtla.github.io/nixtlar/articles/get-started.html)
guide. Here, you can also find a more detailed explanation of how the
forecast and plot functions work.

Using TIMEGPT through `nixtlar` requires a token provided by Nixtla. To
see how to get it and different ways to set it up, please read the [Get
Started](https://nixtla.github.io/nixtlar/articles/get-started.html).
Here you can also find a more detailed explanation on how `nixtlar`’s
forecast and plot function work.

## Main features of `nixtlar`
### Main features of `nixtlar`

- Works with both data frames and
[tsibbles](https://tsibble.tidyverts.org/).
Expand All @@ -82,10 +78,10 @@ forecast and plot function work.

- Includes its own plot function.

Please read the **Articles** on this website or check out the
**Reference** to understand how you can use all these features.
Please read the **Articles** or check out the **Reference** to
understand how you can use all these features.

## Python SDK
### Python SDK

Are you a Python user? If yes, then check out the [Python SDK for
TimeGPT](https://github.com/Nixtla/nixtla).
5 changes: 5 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## R CMD check results

0 errors | 0 warnings | 1 note

* This is a new release.
7 changes: 7 additions & 0 deletions man/date_conversion.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/dot-get_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/dot-timegpt_data_prep.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/infer_frequency.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions man/nixtlaR-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/nixtla_set_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions man/nixtla_validate_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/timegpt_anomaly_detection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/timegpt_cross_validation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/timegpt_forecast.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/timegpt_historic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5812e42

Please sign in to comment.