{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "c67698a1",
"metadata": {},
"source": [
"# Panel regression with JPMaQS (R-code)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8766eddd",
"metadata": {
"papermill": {
"duration": 0.010412,
"end_time": "2023-02-27T10:28:01.241963",
"exception": false,
"start_time": "2023-02-27T10:28:01.231551",
"status": "completed"
},
"tags": []
},
"source": [
"This notebook replicates the results discussed in Macrosynergy's research post on general method for testing the significance of macro trading factors of Feb 11th [\"Testing macro trading factors\"](https://research.macrosynergy.com/testing-macro-trading-factors/). The post uses the example J.P. Morgan Macrosynergy Quantamental System (JPMaQS) set of macro factors available on Kaggle. The importance of using correct time series of macro information, which at any date reflects what the market could have known on that date is discussed [here]( https://www.jpmorgan.com/insights/research/macrosynergy-quantamental-system) "
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4586dff9",
"metadata": {
"papermill": {
"duration": 0.008894,
"end_time": "2023-02-27T10:28:01.259389",
"exception": false,
"start_time": "2023-02-27T10:28:01.250495",
"status": "completed"
},
"tags": []
},
"source": [
"# 1. Loading/attaching relevant packages and JPMaQS dataset and exploring categories and cross-sections in the dataset. "
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "63ba8bfc",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-20T09:38:01.598295Z",
"iopub.status.busy": "2023-02-20T09:38:01.596784Z",
"iopub.status.idle": "2023-02-20T09:38:01.633076Z"
},
"papermill": {
"duration": 0.008449,
"end_time": "2023-02-27T10:28:01.276551",
"exception": false,
"start_time": "2023-02-27T10:28:01.268102",
"status": "completed"
},
"tags": []
},
"source": [
"The description of each JPMaQS category is available either under [JPMorgan Markets](https://markets.jpmorgan.com/#jpmaqs) (password protected, full set of 5000+ indicators) or on [Kaggle](https://www.kaggle.com/datasets/macrosynergy/fixed-income-returns-and-macro-trends) (just for the tickers used in this notebook, no password required)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "386d4767",
"metadata": {
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
"execution": {
"iopub.execute_input": "2023-02-27T10:28:01.299314Z",
"iopub.status.busy": "2023-02-27T10:28:01.296835Z",
"iopub.status.idle": "2023-02-27T10:28:25.027837Z",
"shell.execute_reply": "2023-02-27T10:28:25.025568Z"
},
"papermill": {
"duration": 23.747119,
"end_time": "2023-02-27T10:28:25.032086",
"exception": false,
"start_time": "2023-02-27T10:28:01.284967",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Installing package into ‘/usr/local/lib/R/site-library’\n",
"(as ‘lib’ is unspecified)\n",
"\n"
]
}
],
"source": [
"suppressPackageStartupMessages({\n",
"\n",
"install.packages(\"https://cran.r-project.org/src/contrib/Archive/Zelig/Zelig_4.2-1.tar.gz\", repos=NULL, type=\"source\")\n",
"\n",
"library(plm)\n",
"library(car)\n",
"library(lme4)\n",
"library(gplots) \n",
"library(lubridate) # for aggregation, grouping by\n",
"library(dplyr) # for aggregation, grouping by \n",
"library(zoo)\n",
"library(tidyr) # pivot table\n",
"library(texreg)\n",
"library(Zelig)\n",
"library(tidyverse) # Modern data science library \n",
"library(lmtest) # For hetoroskedasticity analysis\n",
"library(xts)\n",
"library(huxtable)\n",
" \n",
" })"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c067f7a2",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:25.087909Z",
"iopub.status.busy": "2023-02-27T10:28:25.052482Z",
"iopub.status.idle": "2023-02-27T10:28:51.437129Z",
"shell.execute_reply": "2023-02-27T10:28:51.435144Z"
},
"papermill": {
"duration": 26.399386,
"end_time": "2023-02-27T10:28:51.440910",
"exception": false,
"start_time": "2023-02-27T10:28:25.041524",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'data.frame':\t3072750 obs. of 4 variables:\n",
" $ cid : chr \"AUD\" \"AUD\" \"AUD\" \"AUD\" ...\n",
" $ xcat : chr \"CPIC_SA_P1M1ML12\" \"CPIC_SA_P1M1ML12\" \"CPIC_SA_P1M1ML12\" \"CPIC_SA_P1M1ML12\" ...\n",
" $ real_date: chr \"2000-01-03\" \"2000-01-04\" \"2000-01-05\" \"2000-01-06\" ...\n",
" $ value : num 1.24 1.24 1.24 1.24 1.24 ...\n"
]
},
{
"data": {
"text/html": [
"\n",
"
- 'CPIC_SA_P1M1ML12'
- 'CPIC_SJA_P3M3ML3AR'
- 'CPIC_SJA_P6M6ML6AR'
- 'CPIH_SA_P1M1ML12'
- 'CPIH_SJA_P3M3ML3AR'
- 'CPIH_SJA_P6M6ML6AR'
- 'DU02YXR_NSA'
- 'DU02YXR_VT10'
- 'DU05YXR_NSA'
- 'DU05YXR_VT10'
- 'EQXR_NSA'
- 'EQXR_VT10'
- 'FXXR_NSA'
- 'FXXR_VT10'
- 'INFTEFF_NSA'
- 'INTRGDP_NSA_P1M1ML12_3MMA'
- 'INTRGDPv5Y_NSA_P1M1ML12_3MMA'
- 'PCREDITBN_SJA_P1M1ML12'
- 'PCREDITGDP_SJA_D1M1ML12'
- 'RGDP_SA_P1Q1QL4_20QMA'
- 'RYLDIRS02Y_NSA'
- 'RYLDIRS05Y_NSA'
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'CPIC\\_SA\\_P1M1ML12'\n",
"\\item 'CPIC\\_SJA\\_P3M3ML3AR'\n",
"\\item 'CPIC\\_SJA\\_P6M6ML6AR'\n",
"\\item 'CPIH\\_SA\\_P1M1ML12'\n",
"\\item 'CPIH\\_SJA\\_P3M3ML3AR'\n",
"\\item 'CPIH\\_SJA\\_P6M6ML6AR'\n",
"\\item 'DU02YXR\\_NSA'\n",
"\\item 'DU02YXR\\_VT10'\n",
"\\item 'DU05YXR\\_NSA'\n",
"\\item 'DU05YXR\\_VT10'\n",
"\\item 'EQXR\\_NSA'\n",
"\\item 'EQXR\\_VT10'\n",
"\\item 'FXXR\\_NSA'\n",
"\\item 'FXXR\\_VT10'\n",
"\\item 'INFTEFF\\_NSA'\n",
"\\item 'INTRGDP\\_NSA\\_P1M1ML12\\_3MMA'\n",
"\\item 'INTRGDPv5Y\\_NSA\\_P1M1ML12\\_3MMA'\n",
"\\item 'PCREDITBN\\_SJA\\_P1M1ML12'\n",
"\\item 'PCREDITGDP\\_SJA\\_D1M1ML12'\n",
"\\item 'RGDP\\_SA\\_P1Q1QL4\\_20QMA'\n",
"\\item 'RYLDIRS02Y\\_NSA'\n",
"\\item 'RYLDIRS05Y\\_NSA'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'CPIC_SA_P1M1ML12'\n",
"2. 'CPIC_SJA_P3M3ML3AR'\n",
"3. 'CPIC_SJA_P6M6ML6AR'\n",
"4. 'CPIH_SA_P1M1ML12'\n",
"5. 'CPIH_SJA_P3M3ML3AR'\n",
"6. 'CPIH_SJA_P6M6ML6AR'\n",
"7. 'DU02YXR_NSA'\n",
"8. 'DU02YXR_VT10'\n",
"9. 'DU05YXR_NSA'\n",
"10. 'DU05YXR_VT10'\n",
"11. 'EQXR_NSA'\n",
"12. 'EQXR_VT10'\n",
"13. 'FXXR_NSA'\n",
"14. 'FXXR_VT10'\n",
"15. 'INFTEFF_NSA'\n",
"16. 'INTRGDP_NSA_P1M1ML12_3MMA'\n",
"17. 'INTRGDPv5Y_NSA_P1M1ML12_3MMA'\n",
"18. 'PCREDITBN_SJA_P1M1ML12'\n",
"19. 'PCREDITGDP_SJA_D1M1ML12'\n",
"20. 'RGDP_SA_P1Q1QL4_20QMA'\n",
"21. 'RYLDIRS02Y_NSA'\n",
"22. 'RYLDIRS05Y_NSA'\n",
"\n",
"\n"
],
"text/plain": [
" [1] \"CPIC_SA_P1M1ML12\" \"CPIC_SJA_P3M3ML3AR\" \n",
" [3] \"CPIC_SJA_P6M6ML6AR\" \"CPIH_SA_P1M1ML12\" \n",
" [5] \"CPIH_SJA_P3M3ML3AR\" \"CPIH_SJA_P6M6ML6AR\" \n",
" [7] \"DU02YXR_NSA\" \"DU02YXR_VT10\" \n",
" [9] \"DU05YXR_NSA\" \"DU05YXR_VT10\" \n",
"[11] \"EQXR_NSA\" \"EQXR_VT10\" \n",
"[13] \"FXXR_NSA\" \"FXXR_VT10\" \n",
"[15] \"INFTEFF_NSA\" \"INTRGDP_NSA_P1M1ML12_3MMA\" \n",
"[17] \"INTRGDPv5Y_NSA_P1M1ML12_3MMA\" \"PCREDITBN_SJA_P1M1ML12\" \n",
"[19] \"PCREDITGDP_SJA_D1M1ML12\" \"RGDP_SA_P1Q1QL4_20QMA\" \n",
"[21] \"RYLDIRS02Y_NSA\" \"RYLDIRS05Y_NSA\" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"- 'AUD'
- 'CAD'
- 'CHF'
- 'CLP'
- 'COP'
- 'CZK'
- 'EUR'
- 'GBP'
- 'HUF'
- 'IDR'
- 'ILS'
- 'INR'
- 'JPY'
- 'KRW'
- 'MXN'
- 'NOK'
- 'NZD'
- 'PLN'
- 'SEK'
- 'THB'
- 'TRY'
- 'TWD'
- 'USD'
- 'ZAR'
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'AUD'\n",
"\\item 'CAD'\n",
"\\item 'CHF'\n",
"\\item 'CLP'\n",
"\\item 'COP'\n",
"\\item 'CZK'\n",
"\\item 'EUR'\n",
"\\item 'GBP'\n",
"\\item 'HUF'\n",
"\\item 'IDR'\n",
"\\item 'ILS'\n",
"\\item 'INR'\n",
"\\item 'JPY'\n",
"\\item 'KRW'\n",
"\\item 'MXN'\n",
"\\item 'NOK'\n",
"\\item 'NZD'\n",
"\\item 'PLN'\n",
"\\item 'SEK'\n",
"\\item 'THB'\n",
"\\item 'TRY'\n",
"\\item 'TWD'\n",
"\\item 'USD'\n",
"\\item 'ZAR'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'AUD'\n",
"2. 'CAD'\n",
"3. 'CHF'\n",
"4. 'CLP'\n",
"5. 'COP'\n",
"6. 'CZK'\n",
"7. 'EUR'\n",
"8. 'GBP'\n",
"9. 'HUF'\n",
"10. 'IDR'\n",
"11. 'ILS'\n",
"12. 'INR'\n",
"13. 'JPY'\n",
"14. 'KRW'\n",
"15. 'MXN'\n",
"16. 'NOK'\n",
"17. 'NZD'\n",
"18. 'PLN'\n",
"19. 'SEK'\n",
"20. 'THB'\n",
"21. 'TRY'\n",
"22. 'TWD'\n",
"23. 'USD'\n",
"24. 'ZAR'\n",
"\n",
"\n"
],
"text/plain": [
" [1] \"AUD\" \"CAD\" \"CHF\" \"CLP\" \"COP\" \"CZK\" \"EUR\" \"GBP\" \"HUF\" \"IDR\" \"ILS\" \"INR\"\n",
"[13] \"JPY\" \"KRW\" \"MXN\" \"NOK\" \"NZD\" \"PLN\" \"SEK\" \"THB\" \"TRY\" \"TWD\" \"USD\" \"ZAR\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df <- read.csv('../input/fixed-income-returns-and-macro-trends/JPMaQS_Quantamental_Indicators.csv',header=TRUE, stringsAsFactors=FALSE)\n",
"df <- as.data.frame(df)[,c(\"cid\",\"xcat\", \"real_date\", \"value\")] \n",
"rownames(df) <- NULL\n",
"\n",
"str(df)\n",
"unique(df$xcat)\n",
"unique(df$cid)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "a50dc071",
"metadata": {
"papermill": {
"duration": 0.009349,
"end_time": "2023-02-27T10:28:51.463983",
"exception": false,
"start_time": "2023-02-27T10:28:51.454634",
"status": "completed"
},
"tags": []
},
"source": [
"# 2. Prepare data for panel regression analysis"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "da8a2541",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-20T09:38:52.572164Z",
"iopub.status.busy": "2023-02-20T09:38:52.569555Z",
"iopub.status.idle": "2023-02-20T09:38:52.588197Z"
},
"papermill": {
"duration": 0.009316,
"end_time": "2023-02-27T10:28:51.482475",
"exception": false,
"start_time": "2023-02-27T10:28:51.473159",
"status": "completed"
},
"tags": []
},
"source": [
"## 2.1. Specify analysis. Select features, targets, cross-sections and start date\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c77c1353",
"metadata": {
"papermill": {
"duration": 0.008798,
"end_time": "2023-02-27T10:28:51.500031",
"exception": false,
"start_time": "2023-02-27T10:28:51.491233",
"status": "completed"
},
"tags": []
},
"source": [
"In the examples below we use various types of quantamental macroeconomic information at a monthly frequency (month-end status) and relate it to the next month’s 5-year interest rate swap fixed receiver returns for the period 2000 to 2022 and for 6 major developed markets: \n",
"the U.S. (USD), the euro area (EUR), Japan (JPY), the UK (GBP), Canada (CAD), and Australia (AUD). \n",
"The description of each JPMaQS category is available either under [Macro Quantamental Academy](https://academy.macrosynergy.com/quantamental-indicators/), [JPMorgan Markets](https://markets.jpmorgan.com/#jpmaqs) (password protected), or on [Kaggle](https://www.kaggle.com/datasets/macrosynergy/fixed-income-returns-and-macro-trends) (just for the tickers used in this notebook).\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a502a370",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:51.522543Z",
"iopub.status.busy": "2023-02-27T10:28:51.520797Z",
"iopub.status.idle": "2023-02-27T10:28:51.553939Z",
"shell.execute_reply": "2023-02-27T10:28:51.551820Z"
},
"papermill": {
"duration": 0.047932,
"end_time": "2023-02-27T10:28:51.557324",
"exception": false,
"start_time": "2023-02-27T10:28:51.509392",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"feats <- list('CPIC_SJA_P6M6ML6AR', 'RYLDIRS02Y_NSA', \"INTRGDPv5Y_NSA_P1M1ML12_3MMA\")\n",
"targs<- list('DU05YXR_VT10', 'DU05YXR_NSA')\n",
"\n",
"feat1 = feats[1] # 'CPIC_SJA_P6M6ML6AR' as covariate\n",
"feat2 = feats[2] # 'RYLDIRS02Y_NSA' as covariate\n",
"feat3 = feats[3] # 'INTRGDPv5Y_NSA_P1M1ML12_3MMA' as covariate\n",
"\n",
"targ1 = targs[1] # 'DU05YXR_VT10' as target\n",
"targ2 = targs[2] # 'DU05YXR_NSA' as target\n",
"\n",
"\n",
"start_date <- \"2000-01-01\" \n",
"cids_sel <- c('EUR','GBP','USD', 'JPY', 'CAD', 'AUD')\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "ad9c84c6",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-20T09:39:20.195511Z",
"iopub.status.busy": "2023-02-20T09:39:20.191571Z",
"iopub.status.idle": "2023-02-20T09:39:20.215838Z"
},
"papermill": {
"duration": 0.009755,
"end_time": "2023-02-27T10:28:51.579730",
"exception": false,
"start_time": "2023-02-27T10:28:51.569975",
"status": "completed"
},
"tags": []
},
"source": [
"## 2.2. Filter dataset for desired start date and cross-sections"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d0a61c5c",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:51.601979Z",
"iopub.status.busy": "2023-02-27T10:28:51.600160Z",
"iopub.status.idle": "2023-02-27T10:28:53.317797Z",
"shell.execute_reply": "2023-02-27T10:28:53.315579Z"
},
"papermill": {
"duration": 1.731926,
"end_time": "2023-02-27T10:28:53.320862",
"exception": false,
"start_time": "2023-02-27T10:28:51.588936",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"filt_date <- df$real_date > start_date\n",
"filt_cid <- df$cid %in% cids_sel\n",
"\n",
"dfx <- df[filt_date & filt_cid, ]\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "6fd0e22c",
"metadata": {
"papermill": {
"duration": 0.009728,
"end_time": "2023-02-27T10:28:53.339716",
"exception": false,
"start_time": "2023-02-27T10:28:53.329988",
"status": "completed"
},
"tags": []
},
"source": [
"## 2.3. Create lagged features' dataframes\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9787c78e",
"metadata": {
"papermill": {
"duration": 0.00909,
"end_time": "2023-02-27T10:28:53.358138",
"exception": false,
"start_time": "2023-02-27T10:28:53.349048",
"status": "completed"
},
"tags": []
},
"source": [
"The below section selects relevant features, pivots dataframes to wide format, aggregates data in the appropriate form to monthly basis, and lags features by one period, so that we may use the current month's data to forecast next month's returns."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "086ce117",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:53.380464Z",
"iopub.status.busy": "2023-02-27T10:28:53.378756Z",
"iopub.status.idle": "2023-02-27T10:28:53.685233Z",
"shell.execute_reply": "2023-02-27T10:28:53.682862Z"
},
"papermill": {
"duration": 0.321017,
"end_time": "2023-02-27T10:28:53.688284",
"exception": false,
"start_time": "2023-02-27T10:28:53.367267",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"feats = c(feat1, feat2, feat3)\n",
"\n",
"for (i in 1:length(feats)) {\n",
"\n",
" feat = feats[i]\n",
" dfx_feat <- dfx[dfx$xcat == feat, ] # select covariate (feature)\n",
" tbd_feat <- dfx_feat %>% pivot_wider(names_from = cid, values_from = value) # pivot dataframe to wide format\n",
" xdd_feat <- xts(tbd_feat[cids_sel], as.Date(tbd_feat$real_date)) # use real_date as index\n",
" xdm_feat <- period.apply(xdd_feat, endpoints(xdd_feat, on=\"months\"), last) # convert to end-of-month values\n",
" xdm_feat <- stats::lag(xdm_feat, k=1) # lag by 1 period, i.e. delayed use\n",
" real_date <- index(xdm_feat)\n",
" tbm_feat <- cbind(real_date, data.frame(coredata(xdm_feat))) %>% pivot_longer(cols=any_of(cids_sel)) %>% rename(cid = name) # convert back to long data frame\n",
" tbm_feat <- tbm_feat[order(tbm_feat$cid),] # order by cid\n",
" tbm_feat$xcat = paste0(feat, \"_L1M\") # add _L1M to feature name and add the name to created df\n",
" assign(paste0(\"tbm_feat\", i), tbm_feat)\n",
"}"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7c5ccb1e",
"metadata": {
"papermill": {
"duration": 0.009162,
"end_time": "2023-02-27T10:28:53.706801",
"exception": false,
"start_time": "2023-02-27T10:28:53.697639",
"status": "completed"
},
"tags": []
},
"source": [
"## 2.4. Create targets' dataframes\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f0b77741",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:53.728931Z",
"iopub.status.busy": "2023-02-27T10:28:53.727052Z",
"iopub.status.idle": "2023-02-27T10:28:54.573137Z",
"shell.execute_reply": "2023-02-27T10:28:54.571017Z"
},
"papermill": {
"duration": 0.861097,
"end_time": "2023-02-27T10:28:54.576793",
"exception": false,
"start_time": "2023-02-27T10:28:53.715696",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"targs = c(targ1, targ2)\n",
"\n",
"for (i in 1:length(targs)) {\n",
"\n",
" targ = targs[i]\n",
" dfx_targ = dfx[dfx$xcat == targ, ] # select target\n",
" tbd_targ = dfx_targ %>% pivot_wider(names_from = cid, values_from = value) # pivot dataframe to wider format\n",
" xdd_targ = xts(tbd_targ[cids_sel], as.Date(tbd_targ$real_date)) # use real_date as index\n",
" xdm_targ = xts(apply(xdd_targ, 2, apply.monthly, sum), index(xdd_targ)[endpoints(xdd_targ, on=\"months\")]) # convert to monthly sums\n",
" real_date <- index(xdm_targ)\n",
" tbm_targ <- cbind(real_date, data.frame(coredata(xdm_targ))) %>% pivot_longer(cols=any_of(cids_sel)) %>% rename(cid = name) # transform back to long dataframe\n",
" tbm_targ <- tbm_targ[order(tbm_targ$cid),] # order by cid\n",
" tbm_targ$xcat = targ\n",
" assign(paste0(\"tbm_targ\", i), tbm_targ)\n",
"}"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9f32f2c9",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-23T14:41:11.937196Z",
"iopub.status.busy": "2023-02-23T14:41:11.934354Z",
"iopub.status.idle": "2023-02-23T14:41:11.956212Z"
},
"papermill": {
"duration": 0.008817,
"end_time": "2023-02-27T10:28:54.594474",
"exception": false,
"start_time": "2023-02-27T10:28:54.585657",
"status": "completed"
},
"tags": []
},
"source": [
"## 2.4. Create combined dataframes for three regression examples"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "a2f98fd0",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:54.616568Z",
"iopub.status.busy": "2023-02-27T10:28:54.614470Z",
"iopub.status.idle": "2023-02-27T10:28:54.724326Z",
"shell.execute_reply": "2023-02-27T10:28:54.722167Z"
},
"papermill": {
"duration": 0.124746,
"end_time": "2023-02-27T10:28:54.727948",
"exception": false,
"start_time": "2023-02-27T10:28:54.603202",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [],
"source": [
"tbm1 = rbind(tbm_feat1, tbm_targ1) # merge two dataframes. Feature 1, Target 1\n",
"tbx1 = tbm1 %>% pivot_wider(names_from = xcat, values_from = value) # pivot to wide df\n",
"dfx_pan1 <- pdata.frame(tbx1, index=c(\"cid\",\"real_date\"))\n",
"\n",
"tbm2 = rbind(tbm_feat2, tbm_targ2) # merge two dataframes. Feature 2, Target 2\n",
"tbx2 = tbm2 %>% pivot_wider(names_from = xcat, values_from = value) # pivot to wide df\n",
"dfx_pan2 <- pdata.frame(tbx2, index=c(\"cid\",\"real_date\"))\n",
"\n",
"tbm3 = rbind(tbm_feat3, tbm_targ2) # merge two dataframes. Feature 3, Target 2\n",
"tbx3 = tbm3 %>% pivot_wider(names_from = xcat, values_from = value) # pivot to wide df\n",
"dfx_pan3 <- pdata.frame(tbx3, index=c(\"cid\",\"real_date\"))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4b071c22",
"metadata": {
"papermill": {
"duration": 0.008881,
"end_time": "2023-02-27T10:28:54.745726",
"exception": false,
"start_time": "2023-02-27T10:28:54.736845",
"status": "completed"
},
"tags": []
},
"source": [
"# 3. Compare standard pooled regression with time random effects\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3d1aa0ea",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-20T09:42:54.980332Z",
"iopub.status.busy": "2023-02-20T09:42:54.978748Z",
"iopub.status.idle": "2023-02-20T09:42:54.991761Z"
},
"papermill": {
"duration": 0.00885,
"end_time": "2023-02-27T10:28:54.763361",
"exception": false,
"start_time": "2023-02-27T10:28:54.754511",
"status": "completed"
},
"tags": []
},
"source": [
"## 3.1. Example 1: Deflating significance"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8a6970e5",
"metadata": {
"papermill": {
"duration": 0.008765,
"end_time": "2023-02-27T10:28:54.781013",
"exception": false,
"start_time": "2023-02-27T10:28:54.772248",
"status": "completed"
},
"tags": []
},
"source": [
"The simplistic way of checking significance would be to simply stack up all country data and run a pooled regression. Alternatively, one can check the significance of a correlation coefficient for the stacked data set. \n",
"In both cases, the implicit assumption is that the experiences of the different markets are independent and unrelated. By contrast, the method suggested in [\"Testing macro trading factors\"](https://research.macrosynergy.com/testing-macro-trading-factors/) requires us to run a panel regression with period-specific random effects.\n",
"We run our analysis by using the standard `plm` package in R on the quantamental data set. The JPMaQS ticker for the inflation indicator is _CPIC_SJA_P6M6ML6AR_ and target variable is _DU05YXR_VT10_ (description [here](https://www.kaggle.com/datasets/macrosynergy/fixed-income-returns-and-macro-trends)). "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "dd105ba5",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:54.802742Z",
"iopub.status.busy": "2023-02-27T10:28:54.800861Z",
"iopub.status.idle": "2023-02-27T10:28:54.892560Z",
"shell.execute_reply": "2023-02-27T10:28:54.889903Z"
},
"papermill": {
"duration": 0.106026,
"end_time": "2023-02-27T10:28:54.895929",
"exception": false,
"start_time": "2023-02-27T10:28:54.789903",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Pooling Model\n",
"\n",
"Call:\n",
"plm(formula = form1, data = dfx_pan1, model = \"pooling\")\n",
"\n",
"Unbalanced Panel: n = 6, T = 216-277, N = 1583\n",
"\n",
"Residuals:\n",
" Min. 1st Qu. Median 3rd Qu. Max. \n",
"-20.44023 -2.18271 0.16423 2.50893 15.04703 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t-value Pr(>|t|) \n",
"(Intercept) 0.648450 0.169995 3.8145 0.0001417 ***\n",
"CPIC_SJA_P6M6ML6AR_L1M -0.220925 0.081715 -2.7036 0.0069330 ** \n",
"---\n",
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n",
"\n",
"Total Sum of Squares: 23603\n",
"Residual Sum of Squares: 23494\n",
"R-Squared: 0.004602\n",
"Adj. R-Squared: 0.0039724\n",
"F-statistic: 7.3094 on 1 and 1581 DF, p-value: 0.006933"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"form1 = paste(targ1, \"~1 +\", paste0(feat1, \"_L1M\"))\n",
"\n",
"pool1 <- plm(form1, model=\"pooling\", data=dfx_pan1)\n",
"summary(pool1)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0adcf41d",
"metadata": {
"papermill": {
"duration": 0.009229,
"end_time": "2023-02-27T10:28:54.914383",
"exception": false,
"start_time": "2023-02-27T10:28:54.905154",
"status": "completed"
},
"tags": []
},
"source": [
"The pooled regression diagnoses high significance of the 1-month lagged core CPI trend for duration returns. As should be expected the relation between inflation and subsequent duration returns has been negative and the probability of the relationship not being accidental according to the pooled model has been over 99% (indicated by the p-value below being below 0.01.). The post argues, that the chosen inflation indicator is particularly important in the US and in fact, the statistics vastly exaggerates the significance of the chosen indicator for the pool of selected countries. The post argues, that this is a classic example, in which treating countries’ experiences as independent leads to __pseudo-replication__. The results are plausibly misleading with respect to the general importance of core inflation rates across countries. To demonstrate this point we have a look at random period model:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c10c8945",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:54.937012Z",
"iopub.status.busy": "2023-02-27T10:28:54.934991Z",
"iopub.status.idle": "2023-02-27T10:28:55.034874Z",
"shell.execute_reply": "2023-02-27T10:28:55.032124Z"
},
"papermill": {
"duration": 0.115072,
"end_time": "2023-02-27T10:28:55.038600",
"exception": false,
"start_time": "2023-02-27T10:28:54.923528",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Oneway (time) effect Random Effect Model \n",
" (Swamy-Arora's transformation)\n",
"\n",
"Call:\n",
"plm(formula = form1, data = dfx_pan1, effect = \"time\", model = \"random\")\n",
"\n",
"Unbalanced Panel: n = 6, T = 216-277, N = 1583\n",
"\n",
"Effects:\n",
" var std.dev share\n",
"idiosyncratic 6.620 2.573 0.445\n",
"time 8.253 2.873 0.555\n",
"theta:\n",
" Min. 1st Qu. Median Mean 3rd Qu. Max. \n",
" 0.5913 0.6566 0.6566 0.6498 0.6566 0.6566 \n",
"\n",
"Residuals:\n",
" Min. 1st Qu. Median Mean 3rd Qu. Max. \n",
"-12.4009 -1.3821 0.0638 -0.0072 1.5523 10.4376 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error z-value Pr(>|z|) \n",
"(Intercept) 0.490390 0.218245 2.2470 0.02464 *\n",
"CPIC_SJA_P6M6ML6AR_L1M -0.105520 0.068414 -1.5424 0.12298 \n",
"---\n",
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n",
"\n",
"Total Sum of Squares: 10496\n",
"Residual Sum of Squares: 10478\n",
"R-Squared: 0.0017562\n",
"Adj. R-Squared: 0.0011248\n",
"Chisq: 2.3789 on 1 DF, p-value: 0.12298"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rets1 <- plm(form1, model = \"random\", effect = \"time\", data=dfx_pan1)\n",
"summary(rets1)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0b6066ec",
"metadata": {
"papermill": {
"duration": 0.009481,
"end_time": "2023-02-27T10:28:55.057533",
"exception": false,
"start_time": "2023-02-27T10:28:55.048052",
"status": "completed"
},
"tags": []
},
"source": [
"The negative relation between the month-end core CPI trend and next month’s return still shows as negative, but the statistical probability of this relation is only at 87-88%, below the level that many researchers would judge as significant. Also, the theta (degree of effective cross-sectional de-meaning of the data) has been near 2/3, indicating the importance of period-specific effects explaining the returns in the panel and testifying to the risk of pseudo-replication in pools.\n",
"\n",
"For illustration see below period-specfic effects by month for the last 2 years:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "9cc679bb",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:55.080555Z",
"iopub.status.busy": "2023-02-27T10:28:55.078482Z",
"iopub.status.idle": "2023-02-27T10:28:55.113725Z",
"shell.execute_reply": "2023-02-27T10:28:55.108175Z"
},
"papermill": {
"duration": 0.050285,
"end_time": "2023-02-27T10:28:55.117092",
"exception": false,
"start_time": "2023-02-27T10:28:55.066807",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2021-03-31 2021-04-30 2021-05-31 2021-06-30 2021-07-30 2021-08-31 2021-09-30 \n",
" -0.1621 0.4628 -0.0991 -0.3644 2.8663 -0.9117 -5.7688 \n",
"2021-10-29 2021-11-30 2021-12-31 2022-01-31 2022-02-28 2022-03-31 2022-04-29 \n",
" -7.1200 0.4569 -0.9377 -4.4365 -2.0373 -4.7015 -2.4582 \n",
"2022-05-31 2022-06-30 2022-07-29 2022-08-31 2022-09-30 2022-10-31 2022-11-30 \n",
" -0.2491 -2.5410 2.5658 -4.0595 -4.2020 0.3827 1.7394 \n",
"2022-12-30 2023-01-31 2023-02-03 \n",
" -3.3615 2.8229 0.8966 \n"
]
}
],
"source": [
"print(tail(ranef(rets1, effect = \"time\"),24), 3)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "6b0d728d",
"metadata": {
"papermill": {
"duration": 0.009486,
"end_time": "2023-02-27T10:28:55.136348",
"exception": false,
"start_time": "2023-02-27T10:28:55.126862",
"status": "completed"
},
"tags": []
},
"source": [
"## 3.2. Example 2: Confirming significance\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "452bece5",
"metadata": {
"papermill": {
"duration": 0.009813,
"end_time": "2023-02-27T10:28:55.155564",
"exception": false,
"start_time": "2023-02-27T10:28:55.145751",
"status": "completed"
},
"tags": []
},
"source": [
"Using panel-specific regressions can not only help to reject the significance of particular indicators. In fact, it can also help to confirm the significance of particular inficators, shown in the next example. Here we investigate the significance of real Real IRS yield: 2-year maturity measured as the difference between IRS yields and formulaic inflation expectations based on recent CPI trends and effective inflation targets, on duration return. The JPMaQS ticker is _RYLDIRS02Y_NSA_ and we use _DU05YXR_NSA_ as target (description [here](https://www.kaggle.com/datasets/macrosynergy/fixed-income-returns-and-macro-trends)). The pooled regression below shows a very high significance of month-end real yields for subsequent returns. This relation has naturally been positive, with high yields predicting high returns, and with a statistical probability of more than 99.99%."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "df298543",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:55.179527Z",
"iopub.status.busy": "2023-02-27T10:28:55.177492Z",
"iopub.status.idle": "2023-02-27T10:28:55.225508Z",
"shell.execute_reply": "2023-02-27T10:28:55.223372Z"
},
"papermill": {
"duration": 0.062622,
"end_time": "2023-02-27T10:28:55.228122",
"exception": false,
"start_time": "2023-02-27T10:28:55.165500",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Pooling Model\n",
"\n",
"Call:\n",
"plm(formula = form2, data = dfx_pan2, model = \"pooling\")\n",
"\n",
"Unbalanced Panel: n = 6, T = 253-277, N = 1620\n",
"\n",
"Residuals:\n",
" Min. 1st Qu. Median 3rd Qu. Max. \n",
"-7.568227 -0.571400 0.015524 0.570489 5.303937 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t-value Pr(>|t|) \n",
"(Intercept) 0.071400 0.027757 2.5723 0.01019 * \n",
"RYLDIRS02Y_NSA_L1M 0.082863 0.015989 5.1824 2.465e-07 ***\n",
"---\n",
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n",
"\n",
"Total Sum of Squares: 1981.5\n",
"Residual Sum of Squares: 1949.2\n",
"R-Squared: 0.016328\n",
"Adj. R-Squared: 0.01572\n",
"F-statistic: 26.8577 on 1 and 1618 DF, p-value: 2.4651e-07"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"form2 = paste(targ2, \"~1 +\", paste0(feat2, \"_L1M\"))\n",
"pool2 <- plm(form2, model=\"pooling\", data=dfx_pan2)\n",
"summary(pool2)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "031722f8",
"metadata": {
"papermill": {
"duration": 0.00963,
"end_time": "2023-02-27T10:28:55.247293",
"exception": false,
"start_time": "2023-02-27T10:28:55.237663",
"status": "completed"
},
"tags": []
},
"source": [
"We run the period-specific random effects regression to check the significance of the relationship:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1fc2cf91",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:55.270292Z",
"iopub.status.busy": "2023-02-27T10:28:55.268448Z",
"iopub.status.idle": "2023-02-27T10:28:55.351848Z",
"shell.execute_reply": "2023-02-27T10:28:55.349812Z"
},
"papermill": {
"duration": 0.098983,
"end_time": "2023-02-27T10:28:55.355720",
"exception": false,
"start_time": "2023-02-27T10:28:55.256737",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Oneway (time) effect Random Effect Model \n",
" (Swamy-Arora's transformation)\n",
"\n",
"Call:\n",
"plm(formula = form2, data = dfx_pan2, effect = \"time\", model = \"random\")\n",
"\n",
"Unbalanced Panel: n = 6, T = 253-277, N = 1620\n",
"\n",
"Effects:\n",
" var std.dev share\n",
"idiosyncratic 0.5815 0.7626 0.481\n",
"time 0.6266 0.7916 0.519\n",
"theta:\n",
" Min. 1st Qu. Median Mean 3rd Qu. Max. \n",
" 0.5660 0.6340 0.6340 0.6304 0.6340 0.6340 \n",
"\n",
"Residuals:\n",
" Min. 1st Qu. Median Mean 3rd Qu. Max. \n",
"-5.9147 -0.4006 -0.0095 -0.0004 0.4146 3.8556 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error z-value Pr(>|z|) \n",
"(Intercept) 0.077428 0.051608 1.5003 0.1335 \n",
"RYLDIRS02Y_NSA_L1M 0.070252 0.017300 4.0607 4.892e-05 ***\n",
"---\n",
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n",
"\n",
"Total Sum of Squares: 949.22\n",
"Residual Sum of Squares: 939.5\n",
"R-Squared: 0.01024\n",
"Adj. R-Squared: 0.0096283\n",
"Chisq: 16.4895 on 1 DF, p-value: 4.8919e-05"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rets2 <- plm(form2, model = \"random\", effect = \"time\", data=dfx_pan2)\n",
"summary(rets2)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5f09da24",
"metadata": {
"papermill": {
"duration": 0.009739,
"end_time": "2023-02-27T10:28:55.382960",
"exception": false,
"start_time": "2023-02-27T10:28:55.373221",
"status": "completed"
},
"tags": []
},
"source": [
"In this case, the period-specific random effects regression confirms the high significance of the predictor (similar positive coefficient as in pooled regression and statistical probability remains above 99.99%)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "fa3e11ca",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-20T09:50:45.196333Z",
"iopub.status.busy": "2023-02-20T09:50:45.194735Z",
"iopub.status.idle": "2023-02-20T09:50:45.207742Z"
},
"papermill": {
"duration": 0.009708,
"end_time": "2023-02-27T10:28:55.402323",
"exception": false,
"start_time": "2023-02-27T10:28:55.392615",
"status": "completed"
},
"tags": []
},
"source": [
"## 3.3. Example 3: Enhancing significance"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "30029e58",
"metadata": {
"papermill": {
"duration": 0.009625,
"end_time": "2023-02-27T10:28:55.421598",
"exception": false,
"start_time": "2023-02-27T10:28:55.411973",
"status": "completed"
},
"tags": []
},
"source": [
"Sometimes, the significance test based on random effects regression even reinforces evidence of significance provided by simplistic pooled analysis. This plausibly would be the case if relative (de-meaned) values of the features and targets have a close relation. We look at the predictive power of estimated GDP growth trends for duration returns. The JPMaQS ticker is _INTRGDPv5Y_NSA_P1M1ML12_3MMA_ and the same target as in previous example _DU05YXR_NSA_ (description [here](https://www.kaggle.com/datasets/macrosynergy/fixed-income-returns-and-macro-trends)). "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "feda8a61",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:55.445696Z",
"iopub.status.busy": "2023-02-27T10:28:55.443607Z",
"iopub.status.idle": "2023-02-27T10:28:55.493336Z",
"shell.execute_reply": "2023-02-27T10:28:55.490701Z"
},
"papermill": {
"duration": 0.065186,
"end_time": "2023-02-27T10:28:55.496583",
"exception": false,
"start_time": "2023-02-27T10:28:55.431397",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Pooling Model\n",
"\n",
"Call:\n",
"plm(formula = form3, data = dfx_pan3, model = \"pooling\")\n",
"\n",
"Unbalanced Panel: n = 6, T = 259-277, N = 1631\n",
"\n",
"Residuals:\n",
" Min. 1st Qu. Median 3rd Qu. Max. \n",
"-7.6702975 -0.5635270 -0.0053684 0.5804448 5.2240057 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t-value Pr(>|t|) \n",
"(Intercept) 0.090940 0.027681 3.2852 0.001041 **\n",
"INTRGDPv5Y_NSA_P1M1ML12_3MMA_L1M -0.020007 0.010958 -1.8258 0.068065 . \n",
"---\n",
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n",
"\n",
"Total Sum of Squares: 1989.6\n",
"Residual Sum of Squares: 1985.5\n",
"R-Squared: 0.0020422\n",
"Adj. R-Squared: 0.0014295\n",
"F-statistic: 3.3335 on 1 and 1629 DF, p-value: 0.068065"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"form3 = paste(targ2, \"~1 +\", paste0(feat3, \"_L1M\"))\n",
"\n",
"pool3 <- plm(form3, model=\"pooling\", data=dfx_pan3)\n",
"summary(pool3)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "21723ea0",
"metadata": {
"papermill": {
"duration": 0.009864,
"end_time": "2023-02-27T10:28:55.516310",
"exception": false,
"start_time": "2023-02-27T10:28:55.506446",
"status": "completed"
},
"tags": []
},
"source": [
"Running period-specific random effects regression in fact strenghens the significance of growth trends: Indeed, period-specific random effects regression here shows a slightly higher significance of the predictor. Statistical probability around 95%."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a0ee1446",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:55.540562Z",
"iopub.status.busy": "2023-02-27T10:28:55.538425Z",
"iopub.status.idle": "2023-02-27T10:28:55.625509Z",
"shell.execute_reply": "2023-02-27T10:28:55.622829Z"
},
"papermill": {
"duration": 0.102696,
"end_time": "2023-02-27T10:28:55.628833",
"exception": false,
"start_time": "2023-02-27T10:28:55.526137",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Oneway (time) effect Random Effect Model \n",
" (Swamy-Arora's transformation)\n",
"\n",
"Call:\n",
"plm(formula = form3, data = dfx_pan3, effect = \"time\", model = \"random\")\n",
"\n",
"Unbalanced Panel: n = 6, T = 259-277, N = 1631\n",
"\n",
"Effects:\n",
" var std.dev share\n",
"idiosyncratic 0.5834 0.7638 0.477\n",
"time 0.6396 0.7998 0.523\n",
"theta:\n",
" Min. 1st Qu. Median Mean 3rd Qu. Max. \n",
" 0.5691 0.6368 0.6368 0.6341 0.6368 0.6368 \n",
"\n",
"Residuals:\n",
" Min. 1st Qu. Median Mean 3rd Qu. Max. \n",
"-5.9494 -0.3883 -0.0118 -0.0012 0.4157 3.9001 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error z-value Pr(>|z|) \n",
"(Intercept) 0.095441 0.051829 1.8415 0.06555 .\n",
"INTRGDPv5Y_NSA_P1M1ML12_3MMA_L1M -0.024132 0.012348 -1.9543 0.05066 .\n",
"---\n",
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n",
"\n",
"Total Sum of Squares: 950.93\n",
"Residual Sum of Squares: 948.56\n",
"R-Squared: 0.0024934\n",
"Adj. R-Squared: 0.001881\n",
"Chisq: 3.81943 on 1 DF, p-value: 0.050662"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rets3 <- plm(form3, model=\"random\", effect=\"time\", data=dfx_pan3)\n",
"summary(rets3)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0347033f",
"metadata": {
"papermill": {
"duration": 0.009911,
"end_time": "2023-02-27T10:28:55.648758",
"exception": false,
"start_time": "2023-02-27T10:28:55.638847",
"status": "completed"
},
"tags": []
},
"source": [
"Growth trends are often better indicators of relative country performance, because they are comparable and fairly exact, as opposed to survey data and early indicators, which are timelier and better suited to track changes to the absolute growth performance of a particular country. Hence, the consideration of period-specific effects, which partly look at relative country performance and returns, could plausibly reinforce their importance.\n",
"\n",
"Indeed, period-specific random effects regression here shows a slightly higher significance of the predictor. Statistical probability is close to 95%.\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "fd37d088",
"metadata": {
"papermill": {
"duration": 0.010188,
"end_time": "2023-02-27T10:28:55.669048",
"exception": false,
"start_time": "2023-02-27T10:28:55.658860",
"status": "completed"
},
"tags": []
},
"source": [
"# 4. Summarizing the regressions"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "b3e43c6d",
"metadata": {
"papermill": {
"duration": 0.010083,
"end_time": "2023-02-27T10:28:55.689249",
"exception": false,
"start_time": "2023-02-27T10:28:55.679166",
"status": "completed"
},
"tags": []
},
"source": [
"R offers various packages for displaying result of the regressions in a table, for example `huxreg`."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "ee0e6105",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-20T10:25:48.612771Z",
"iopub.status.busy": "2023-02-20T10:25:48.611306Z",
"iopub.status.idle": "2023-02-20T10:25:48.645815Z"
},
"papermill": {
"duration": 0.0104,
"end_time": "2023-02-27T10:28:55.709812",
"exception": false,
"start_time": "2023-02-27T10:28:55.699412",
"status": "completed"
},
"tags": []
},
"source": [
"## 4.1. Example 1: Deflating significance with random time effects"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "bb067b34",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:55.734874Z",
"iopub.status.busy": "2023-02-27T10:28:55.732699Z",
"iopub.status.idle": "2023-02-27T10:28:56.129206Z",
"shell.execute_reply": "2023-02-27T10:28:56.126374Z"
},
"papermill": {
"duration": 0.411935,
"end_time": "2023-02-27T10:28:56.132117",
"exception": false,
"start_time": "2023-02-27T10:28:55.720182",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"A huxtable: 7 × 3\n",
"\n",
"\t | names | Pool | Time Random |
\n",
"\t | <chr> | <chr> | <chr> |
\n",
"\n",
"\n",
"\t | | Pool | Time Random |
\n",
"\t| 1 | (Intercept) | 0.648450039311764 *** | 0.490390219003394 ** |
\n",
"\t| 2 | | [3.81452774975842] | [2.24697033688216] |
\n",
"\t| 3 | CPIC_SJA_P6M6ML6AR_L1M | -0.220924934337292 *** | -0.105519540253456 |
\n",
"\t| 4 | | [-2.70359030753287] | [-1.54236973778011] |
\n",
"\t| 1.1 | P-value | 0.00693298007520692 | 0.122983771084268 |
\n",
"\t| .1 | *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. | *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. | *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. |
\n",
"\n",
"
\n"
],
"text/latex": [
"A huxtable: 7 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & names & Pool & Time Random\\\\\n",
" & & & \\\\\n",
"\\hline\n",
"\t & & Pool & Time Random \\\\\n",
"\t1 & (Intercept) & 0.648450039311764 *** & 0.490390219003394 ** \\\\\n",
"\t2 & & {[}3.81452774975842{]} & {[}2.24697033688216{]} \\\\\n",
"\t3 & CPIC\\_SJA\\_P6M6ML6AR\\_L1M & -0.220924934337292 *** & -0.105519540253456 \\\\\n",
"\t4 & & {[}-2.70359030753287{]} & {[}-1.54236973778011{]} \\\\\n",
"\t1.1 & P-value & 0.00693298007520692 & 0.122983771084268 \\\\\n",
"\t.1 & *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. & *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. & *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets.\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A huxtable: 7 × 3\n",
"\n",
"| | names <chr> | Pool <chr> | Time Random <chr> |\n",
"|---|---|---|---|\n",
"| | | Pool | Time Random |\n",
"| 1 | (Intercept) | 0.648450039311764 *** | 0.490390219003394 ** |\n",
"| 2 | | [3.81452774975842] | [2.24697033688216] |\n",
"| 3 | CPIC_SJA_P6M6ML6AR_L1M | -0.220924934337292 *** | -0.105519540253456 |\n",
"| 4 | | [-2.70359030753287] | [-1.54236973778011] |\n",
"| 1.1 | P-value | 0.00693298007520692 | 0.122983771084268 |\n",
"| .1 | *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. | *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. | *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets. |\n",
"\n"
],
"text/plain": [
" names \n",
" \n",
"1 (Intercept) \n",
"2 \n",
"3 CPIC_SJA_P6M6ML6AR_L1M \n",
"4 \n",
"1.1 P-value \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets.\n",
" Pool \n",
" Pool \n",
"1 0.648450039311764 *** \n",
"2 [3.81452774975842] \n",
"3 -0.220924934337292 *** \n",
"4 [-2.70359030753287] \n",
"1.1 0.00693298007520692 \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets.\n",
" Time Random \n",
" Time Random \n",
"1 0.490390219003394 ** \n",
"2 [2.24697033688216] \n",
"3 -0.105519540253456 \n",
"4 [-1.54236973778011] \n",
"1.1 0.122983771084268 \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1. T statistics in brackets."
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\n",
"huxreg(\"Pool\" = pool1, \"Time Random\" = rets1, error_pos = \"below\", stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01), statistics = c('P-value' = 'p.value'), number_format = \"%.3f\", error_format = \"[{statistic}]\", \n",
" note = \"{stars}. T statistics in brackets.\")\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "22c30006",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-23T12:27:04.847342Z",
"iopub.status.busy": "2023-02-23T12:27:04.845411Z",
"iopub.status.idle": "2023-02-23T12:27:04.860590Z"
},
"papermill": {
"duration": 0.010603,
"end_time": "2023-02-27T10:28:56.154321",
"exception": false,
"start_time": "2023-02-27T10:28:56.143718",
"status": "completed"
},
"tags": []
},
"source": [
"## 4.2. Example 2: Confirming significance with random time effects"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "980af290",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:56.181390Z",
"iopub.status.busy": "2023-02-27T10:28:56.179340Z",
"iopub.status.idle": "2023-02-27T10:28:56.309251Z",
"shell.execute_reply": "2023-02-27T10:28:56.307032Z"
},
"papermill": {
"duration": 0.146409,
"end_time": "2023-02-27T10:28:56.312143",
"exception": false,
"start_time": "2023-02-27T10:28:56.165734",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"A huxtable: 7 × 3\n",
"\n",
"\t | names | Pool | Time Random |
\n",
"\t | <chr> | <chr> | <chr> |
\n",
"\n",
"\n",
"\t | | Pool | Time Random |
\n",
"\t| 1 | (Intercept) | 0.0714000407995654 ** | 0.077428381691158 |
\n",
"\t| 2 | | (0.0277569633002538) | (0.0516083589903508) |
\n",
"\t| 3 | RYLDIRS02Y_NSA_L1M | 0.0828626212726093 *** | 0.0702523956115305 *** |
\n",
"\t| 4 | | (0.0159890971135926) | (0.0173004285290142) |
\n",
"\t| 1.1 | P-value | 2.46509730904769e-07 | 4.89191979781359e-05 |
\n",
"\t| .1 | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. |
\n",
"\n",
"
\n"
],
"text/latex": [
"A huxtable: 7 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & names & Pool & Time Random\\\\\n",
" & & & \\\\\n",
"\\hline\n",
"\t & & Pool & Time Random \\\\\n",
"\t1 & (Intercept) & 0.0714000407995654 ** & 0.077428381691158 \\\\\n",
"\t2 & & (0.0277569633002538) & (0.0516083589903508) \\\\\n",
"\t3 & RYLDIRS02Y\\_NSA\\_L1M & 0.0828626212726093 *** & 0.0702523956115305 *** \\\\\n",
"\t4 & & (0.0159890971135926) & (0.0173004285290142) \\\\\n",
"\t1.1 & P-value & 2.46509730904769e-07 & 4.89191979781359e-05 \\\\\n",
"\t.1 & *** p < 0.01; ** p < 0.05; * p < 0.1. & *** p < 0.01; ** p < 0.05; * p < 0.1. & *** p < 0.01; ** p < 0.05; * p < 0.1.\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A huxtable: 7 × 3\n",
"\n",
"| | names <chr> | Pool <chr> | Time Random <chr> |\n",
"|---|---|---|---|\n",
"| | | Pool | Time Random |\n",
"| 1 | (Intercept) | 0.0714000407995654 ** | 0.077428381691158 |\n",
"| 2 | | (0.0277569633002538) | (0.0516083589903508) |\n",
"| 3 | RYLDIRS02Y_NSA_L1M | 0.0828626212726093 *** | 0.0702523956115305 *** |\n",
"| 4 | | (0.0159890971135926) | (0.0173004285290142) |\n",
"| 1.1 | P-value | 2.46509730904769e-07 | 4.89191979781359e-05 |\n",
"| .1 | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. |\n",
"\n"
],
"text/plain": [
" names \n",
" \n",
"1 (Intercept) \n",
"2 \n",
"3 RYLDIRS02Y_NSA_L1M \n",
"4 \n",
"1.1 P-value \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1.\n",
" Pool \n",
" Pool \n",
"1 0.0714000407995654 ** \n",
"2 (0.0277569633002538) \n",
"3 0.0828626212726093 *** \n",
"4 (0.0159890971135926) \n",
"1.1 2.46509730904769e-07 \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1.\n",
" Time Random \n",
" Time Random \n",
"1 0.077428381691158 \n",
"2 (0.0516083589903508) \n",
"3 0.0702523956115305 *** \n",
"4 (0.0173004285290142) \n",
"1.1 4.89191979781359e-05 \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1."
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"huxreg(\"Pool\" = pool2, \"Time Random\" = rets2, error_pos = \"below\", stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01), statistics = c('P-value' = 'p.value'), number_format = \"%.3f\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "1457187a",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-20T10:31:38.254581Z",
"iopub.status.busy": "2023-02-20T10:31:38.252813Z",
"iopub.status.idle": "2023-02-20T10:31:38.269205Z"
},
"papermill": {
"duration": 0.010763,
"end_time": "2023-02-27T10:28:56.333943",
"exception": false,
"start_time": "2023-02-27T10:28:56.323180",
"status": "completed"
},
"tags": []
},
"source": [
"## 4.3. Example 3: Enhancing significance with random time effects"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "8b5f8cf8",
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-27T10:28:56.359929Z",
"iopub.status.busy": "2023-02-27T10:28:56.357683Z",
"iopub.status.idle": "2023-02-27T10:28:56.484700Z",
"shell.execute_reply": "2023-02-27T10:28:56.482644Z"
},
"papermill": {
"duration": 0.142724,
"end_time": "2023-02-27T10:28:56.487410",
"exception": false,
"start_time": "2023-02-27T10:28:56.344686",
"status": "completed"
},
"tags": [],
"vscode": {
"languageId": "r"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"A huxtable: 7 × 3\n",
"\n",
"\t | names | Pool | Time Random |
\n",
"\t | <chr> | <chr> | <chr> |
\n",
"\n",
"\n",
"\t | | Pool | Time Random |
\n",
"\t| 1 | (Intercept) | 0.0909395750031496 *** | 0.095441166375607 * |
\n",
"\t| 2 | | (0.0276812642325732) | (0.0518291599921125) |
\n",
"\t| 3 | INTRGDPv5Y_NSA_P1M1ML12_3MMA_L1M | -0.0200069982009708 * | -0.024132192811326 * |
\n",
"\t| 4 | | (0.0109580138249196) | (0.0123480293844477) |
\n",
"\t| 1.1 | P-value | 0.0680653538559699 | 0.0506615471228162 |
\n",
"\t| .1 | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. |
\n",
"\n",
"
\n"
],
"text/latex": [
"A huxtable: 7 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & names & Pool & Time Random\\\\\n",
" & & & \\\\\n",
"\\hline\n",
"\t & & Pool & Time Random \\\\\n",
"\t1 & (Intercept) & 0.0909395750031496 *** & 0.095441166375607 * \\\\\n",
"\t2 & & (0.0276812642325732) & (0.0518291599921125) \\\\\n",
"\t3 & INTRGDPv5Y\\_NSA\\_P1M1ML12\\_3MMA\\_L1M & -0.0200069982009708 * & -0.024132192811326 * \\\\\n",
"\t4 & & (0.0109580138249196) & (0.0123480293844477) \\\\\n",
"\t1.1 & P-value & 0.0680653538559699 & 0.0506615471228162 \\\\\n",
"\t.1 & *** p < 0.01; ** p < 0.05; * p < 0.1. & *** p < 0.01; ** p < 0.05; * p < 0.1. & *** p < 0.01; ** p < 0.05; * p < 0.1.\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A huxtable: 7 × 3\n",
"\n",
"| | names <chr> | Pool <chr> | Time Random <chr> |\n",
"|---|---|---|---|\n",
"| | | Pool | Time Random |\n",
"| 1 | (Intercept) | 0.0909395750031496 *** | 0.095441166375607 * |\n",
"| 2 | | (0.0276812642325732) | (0.0518291599921125) |\n",
"| 3 | INTRGDPv5Y_NSA_P1M1ML12_3MMA_L1M | -0.0200069982009708 * | -0.024132192811326 * |\n",
"| 4 | | (0.0109580138249196) | (0.0123480293844477) |\n",
"| 1.1 | P-value | 0.0680653538559699 | 0.0506615471228162 |\n",
"| .1 | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. | *** p < 0.01; ** p < 0.05; * p < 0.1. |\n",
"\n"
],
"text/plain": [
" names \n",
" \n",
"1 (Intercept) \n",
"2 \n",
"3 INTRGDPv5Y_NSA_P1M1ML12_3MMA_L1M \n",
"4 \n",
"1.1 P-value \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1.\n",
" Pool \n",
" Pool \n",
"1 0.0909395750031496 *** \n",
"2 (0.0276812642325732) \n",
"3 -0.0200069982009708 * \n",
"4 (0.0109580138249196) \n",
"1.1 0.0680653538559699 \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1.\n",
" Time Random \n",
" Time Random \n",
"1 0.095441166375607 * \n",
"2 (0.0518291599921125) \n",
"3 -0.024132192811326 * \n",
"4 (0.0123480293844477) \n",
"1.1 0.0506615471228162 \n",
".1 *** p < 0.01; ** p < 0.05; * p < 0.1."
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"huxreg(\"Pool\" = pool3, \"Time Random\" = rets3, error_pos = \"below\", stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01), statistics = c('P-value' = 'p.value'), number_format = \"%.3f\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bf86a284",
"metadata": {
"papermill": {
"duration": 0.010768,
"end_time": "2023-02-27T10:28:56.567568",
"exception": false,
"start_time": "2023-02-27T10:28:56.556800",
"status": "completed"
},
"tags": []
},
"source": [
"The END"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.0.5"
},
"papermill": {
"default_parameters": {},
"duration": 59.405189,
"end_time": "2023-02-27T10:28:56.701685",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2023-02-27T10:27:57.296496",
"version": "2.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}