create_time_trends: Fast creatation of time trends

View source: R/create_time_trends.R

create_time_trendsR Documentation

Fast creatation of time trends

Description

create_time_trends() quickly creates time trends based on a character columns from inputted data. This function is useful for statistical analysis when you want time trends. The function will only operate on 'data.tables' by reference.

Usage

create_time_trends(
  .data,
  time.col,
  select.column,
  quadratic.time.trends = FALSE,
  remove.first.trend = FALSE
)

Arguments

.data

a data.table

time.col

a time column; can be of any type. 'time.col' will be ordered using order()

select.column

a column with cross-sectional units that will be used to create the time trends. Think fixed effects such as cities, regions, counties, etc.

quadratic.time.trends

a logical for whether to include quadratic (squared) time trends __IN ADDITION__ to the linear time trends for the the dummy columns. Default is FALSE

remove.first.trend

removes the first time trend so that only N - 1 time trends reamin. This avoids the multicollinearity in models. Default is FALSE

Value

A data.table with the same number of rows, but the time trends included. The format of the time trend columns will be tt.select.column_value (linear trends) or tt2.select.column_value (quadratic trends) where select.column is the name of the character column requested from the user and value is the value of the of the character column for that time trend

See Also

dummy_cols For creating dummy columns

Other dummy functions: dummy_cols(), dummy_rows()

Examples

## For unicode meanings, see
## https://gist.github.com/ChandlerLutz/cca06fb35003713efae87c0322ac193e
clean_readxl_backslashes("\\n")
clean_readxl_backslashes("\\u00ad")
clean_readxl_backslashes("\\u2013")
clean_readxl_backslashes("\\u2013")
crime <- setDT(expand.grid(city = c("SF", "SF", "NYC"),
               year = c(1990, 2000, 2010),
               crime = 1:3, stringsAsFactors = FALSE))
crime2 <- copy(crime)
print(create_time_trends(crime2, time.col = "year",
                         select.column = "city"))

ChandlerLutz/CLmisc documentation built on Dec. 2, 2022, 12:40 p.m.