---
title: "Getting started with the cansim package"
description: >
  This vignette provides an overview of the `cansim` package, including how to install it, how to retrieve data from Statistics Canada's NDM, and how to work with the data.
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting started with the cansim package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
	collapse = TRUE,
	comment = "#>",
  fig.width = 7,
  fig.height = 5,
	cache = FALSE,
	eval = nzchar(Sys.getenv("COMPILE_VIG"))
)
library(cansim)
```

### About

The `cansim` package provides R bindings to [Statistics Canada’s main socioeconomic time series database](https://www150.statcan.gc.ca/n1/en/type/data), previously known as (and frequently referred to in this package, and elsewhere, as) CANSIM. Data can be accessed by table number, vector or both table number and coordinate. The package accepts both old and new (NDM) CANSIM table catalogue numbers.

### Installing cansim

The `cansim` package is available on CRAN and can be installed directly using the default package installation process:
```{r, eval = FALSE}
install.packages("cansim")
```
Alternatively, the latest development version of the package can be downloaded from [Github](https://github.com/mountainMath/cansim) using the [devtools](https://cran.r-project.org/package=devtools) or [remotes](https://cran.r-project.org/package=remotes) packages.
```{r load_package_cran, echo=TRUE, message=FALSE, warning=FALSE, eval = FALSE}
# install.packages("remotes")
remotes::install_github("mountainmath/cansim")

library(cansim)
```

### Usage

If you know the data table catalogue number you are interested in, use `get_cansim` to download the entire table.
```{r}
data <- get_cansim("14-10-0293")
head(data)
```
By default, the data tables retrieved by the package comes in the original format provided by Statistics Canada and is enriched by several added columns and transformations.

* An additional `Date` column is added that tries to intelligently infer a Date object from the `REF_DATE` column.
* An additional `val_norm` column is added, that applies the appropriate scaling factor to the `VALUE` column. So if data is coded as "thousands of dollars", a value of `2.4` in the `VALUE` column is converted to a value of `2400` in the `val_norm` column. Similarly, a percentage of `12.2` in the `VALUE` column is converted to a value of `0.122` in the `val_norm` column.
* Categorical variables are converted to factors and, if necessarily, de-duplicated by appending the name of the "parent" category in parenthesis. This ensures that column variables are unique and that they retain their original ordering.

Taking a look at an overview of the data within a table is a common first step. This is implemented in the package with the `get_cansim_table_overview(table_number)` function.
```{r}
get_cansim_table_overview("14-10-0293")
```
When a table number is unknown, you can browse the available tables or search by survey name, keyword or title.
```{r}
search_cansim_cubes("housing price indexes")
```
Individual series in Statistics Canada data tables can also be accessed by using individual numbered vectors. This is especially useful when building reports using specific indicators. For convenience, the `cansim` package allows users to specify named vectors, where the `label` field will be added to the returned data frame containing the specified name for each vector.
```{r}
get_cansim_vector(c("Metro Van Apartment Construction Price Index"="v44176267",
                    "Metro Van CPI"="v41692930"),
                  start_time = "2015-05-01",
                  end_time="2015-08-01") |>
  dplyr::select(Date,GEO,label,VALUE,val_norm)
```

### License

The code in this package is licensed under the MIT license. The bundled table metadata in Sysdata.R, as well as all Statistics Canada data retrieved using this package is made available under the Statistics Canada Open Licence Agreement, a copy of which is included in the R folder. The Statistics Canada Open Licence Agreement requires that:

```
Subject to this agreement, Statistics Canada grants you a worldwide, royalty-free, non-exclusive licence to:

  - use, reproduce, publish, freely distribute, or sell the Information;
  - use, reproduce, publish, freely distribute, or sell Value-added Products; and,
  - sublicence any or all such rights, under terms consistent with this agreement.

In doing any of the above, you shall:

  - reproduce the Information accurately;
  - not use the Information in a way that suggests that Statistics Canada endorses you or your use of the Information;
  - not misrepresent the Information or its source;
  - use the Information in a manner that does not breach or infringe any applicable laws;
  - not merge or link the Information with any other databases for the purpose of attempting to identify an individual person, business or organization; and
  - not present the Information in such a manner that gives the appearance that you may have received, or had access to, information held by Statistics Canada about any identifiable individual person, business or organization.
```

### Attribution

Subject to the Statistics Canada Open Licence Agreement, licensed products using Statistics Canada data should employ the following acknowledgement of source:

```
Acknowledgment of Source

(a) You shall include and maintain the following notice on all licensed rights of the Information:

  - Source: Statistics Canada, name of product, reference date. Reproduced and distributed on an "as is" basis with the permission of Statistics Canada.

(b) Where any Information is contained within a Value-added Product, you shall include on such Value-added Product the following notice:

  - Adapted from Statistics Canada, name of product, reference date. This does not constitute an endorsement by Statistics Canada of this product.
```


