Scientific-Statistics-QA / validation.jsonl
RegalFire's picture
Publish verified RegalFire scientific product v2
3f6db23 verified
Raw History Blame Contribute Delete
401 kB
{"accepted_answer": {"answer_html": "<p>The only requirement for a hazard as a function of time is that it be non-negative and integrate to infinity in the limit of infinite time. See the <a href=\"https://en.wikipedia.org/wiki/Survival_analysis#Hazard_function_and_cumulative_hazard_function\" rel=\"nofollow noreferrer\">Wikipedia entry</a>. What you propose is thus possible, in principle.</p>\n<p>The difficulty is that you would no longer have a proportional hazards model. Consider the hazard ratio for individuals <span class=\"math-container\">$i$</span> and <span class=\"math-container\">$j$</span> at time <span class=\"math-container\">$t$</span> in your additive proposal:</p>\n<p><span class=\"math-container\">$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}}{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}. $$</span></p>\n<p>Compare that to a model with multiplicative hazards:</p>\n<p><span class=\"math-container\">$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}} {\\lambda_1\\gamma_1 t^{\\gamma_1-1}\\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}=\\frac{U_i(t)^{\\gamma_2-1}}{U_j(t)^{\\gamma_2-1}}. $$</span></p>\n<p>Unlike your additive suggestion, with the multiplicative hazards the hazard ratio between individuals <span class=\"math-container\">$i$</span> and <span class=\"math-container\">$j$</span> is constant over time, given their values of <span class=\"math-container\">$U(t)$</span>.</p>\n<p>A proportional hazards model has a particular advantage in that it only depends on the covariate values in place at event times. Other types of models, like accelerated-failure-time models other than Weibull, depend on the entire history of time-varying covariate values like <span class=\"math-container\">$U(t)$</span>. That's one reason for favoring multiplicative hazards.</p>\n", "answer_id": 676869, "answer_text": "The only requirement for a hazard as a function of time is that it be non-negative and integrate to infinity in the limit of infinite time. See the Wikipedia entry (https://en.wikipedia.org/wiki/Survival_analysis#Hazard_function_and_cumulative_hazard_function). What you propose is thus possible, in principle.\n\n\n\n\nThe difficulty is that you would no longer have a proportional hazards model. Consider the hazard ratio for individuals $i$ and $j$ at time $t$ in your additive proposal:\n\n\n\n\n$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}}{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}. $$\n\n\n\n\nCompare that to a model with multiplicative hazards:\n\n\n\n\n$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}} {\\lambda_1\\gamma_1 t^{\\gamma_1-1}\\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}=\\frac{U_i(t)^{\\gamma_2-1}}{U_j(t)^{\\gamma_2-1}}. $$\n\n\n\n\nUnlike your additive suggestion, with the multiplicative hazards the hazard ratio between individuals $i$ and $j$ is constant over time, given their values of $U(t)$.\n\n\n\n\nA proportional hazards model has a particular advantage in that it only depends on the covariate values in place at event times. Other types of models, like accelerated-failure-time models other than Weibull, depend on the entire history of time-varying covariate values like $U(t)$. That's one reason for favoring multiplicative hazards.", "answer_url": "https://stats.stackexchange.com/a/676869", "author": "EdM", "author_url": "https://stats.stackexchange.com/users/28500/edm", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-14T15:23:45+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676850, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "EdM", "profile_url": "https://stats.stackexchange.com/users/28500/edm", "user_type": "registered"}, "created_at": "2026-08-14T15:23:45+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "3D1D9A3E-6087-4625-BCEE-EC8203D245D9", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/3D1D9A3E-6087-4625-BCEE-EC8203D245D9/view-source"}], "score": 4, "updated_at": "2026-08-14T15:23:45+00:00"}, "other_answers": [], "product": "statistics_qa", "question": "An object is manufactured on some date. There are two things which contribute to the risk of failure: time passed since the date of manufacture and the amount of cumulative usage.\n\n\n\n\nAssuming there is data on multiple objects at different time points, a parametric PH model can be used ($t$ = time since manufacture, $U(t)$ = cumulative usage up to time $t$):\n\n\n\n\n$$h(t \\mid U) = \\lambda \\gamma t^{\\gamma-1}\\,\\exp\\!\\big(\\beta_1 U(t)\\big)$$\n\n\n\n\nIs it possible to define a model where two hazard functions are added for the hazard from time and hazard from cumulative usage?\n\n\n\n\n$$h(t) = \\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U(t)^{\\gamma_2-1}$$\n\n\n\n\nTypically, I have seen additive hazard models that are additive in terms of covariates (Aalen additive hazards model):\n\n\n\n\n$$\\lambda(t \\mid X) = \\beta_0(t) + \\beta_1(t) X_1 + \\beta_2(t) X_2 + \\cdots + \\beta_p(t) X_p$$\n\n\n\n\nBut is it possible to have a model that is additive in the hazard contributions of two time scales?\n\n\n\n\n$$h(t) = \\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U(t)^{\\gamma_2-1}$$", "record_id": "Scientific-Statistics-QA:stats:676850", "selected_answer": {"answer_html": "<p>The only requirement for a hazard as a function of time is that it be non-negative and integrate to infinity in the limit of infinite time. See the <a href=\"https://en.wikipedia.org/wiki/Survival_analysis#Hazard_function_and_cumulative_hazard_function\" rel=\"nofollow noreferrer\">Wikipedia entry</a>. What you propose is thus possible, in principle.</p>\n<p>The difficulty is that you would no longer have a proportional hazards model. Consider the hazard ratio for individuals <span class=\"math-container\">$i$</span> and <span class=\"math-container\">$j$</span> at time <span class=\"math-container\">$t$</span> in your additive proposal:</p>\n<p><span class=\"math-container\">$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}}{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}. $$</span></p>\n<p>Compare that to a model with multiplicative hazards:</p>\n<p><span class=\"math-container\">$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}} {\\lambda_1\\gamma_1 t^{\\gamma_1-1}\\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}=\\frac{U_i(t)^{\\gamma_2-1}}{U_j(t)^{\\gamma_2-1}}. $$</span></p>\n<p>Unlike your additive suggestion, with the multiplicative hazards the hazard ratio between individuals <span class=\"math-container\">$i$</span> and <span class=\"math-container\">$j$</span> is constant over time, given their values of <span class=\"math-container\">$U(t)$</span>.</p>\n<p>A proportional hazards model has a particular advantage in that it only depends on the covariate values in place at event times. Other types of models, like accelerated-failure-time models other than Weibull, depend on the entire history of time-varying covariate values like <span class=\"math-container\">$U(t)$</span>. That's one reason for favoring multiplicative hazards.</p>\n", "answer_id": 676869, "answer_text": "The only requirement for a hazard as a function of time is that it be non-negative and integrate to infinity in the limit of infinite time. See the Wikipedia entry (https://en.wikipedia.org/wiki/Survival_analysis#Hazard_function_and_cumulative_hazard_function). What you propose is thus possible, in principle.\n\n\n\n\nThe difficulty is that you would no longer have a proportional hazards model. Consider the hazard ratio for individuals $i$ and $j$ at time $t$ in your additive proposal:\n\n\n\n\n$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}}{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}. $$\n\n\n\n\nCompare that to a model with multiplicative hazards:\n\n\n\n\n$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}} {\\lambda_1\\gamma_1 t^{\\gamma_1-1}\\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}=\\frac{U_i(t)^{\\gamma_2-1}}{U_j(t)^{\\gamma_2-1}}. $$\n\n\n\n\nUnlike your additive suggestion, with the multiplicative hazards the hazard ratio between individuals $i$ and $j$ is constant over time, given their values of $U(t)$.\n\n\n\n\nA proportional hazards model has a particular advantage in that it only depends on the covariate values in place at event times. Other types of models, like accelerated-failure-time models other than Weibull, depend on the entire history of time-varying covariate values like $U(t)$. That's one reason for favoring multiplicative hazards.", "answer_url": "https://stats.stackexchange.com/a/676869", "author": "EdM", "author_url": "https://stats.stackexchange.com/users/28500/edm", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-14T15:23:45+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676850, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "EdM", "profile_url": "https://stats.stackexchange.com/users/28500/edm", "user_type": "registered"}, "created_at": "2026-08-14T15:23:45+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "3D1D9A3E-6087-4625-BCEE-EC8203D245D9", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/3D1D9A3E-6087-4625-BCEE-EC8203D245D9/view-source"}], "score": 4, "updated_at": "2026-08-14T15:23:45+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": 676869, "answers": [{"answer_html": "<p>The only requirement for a hazard as a function of time is that it be non-negative and integrate to infinity in the limit of infinite time. See the <a href=\"https://en.wikipedia.org/wiki/Survival_analysis#Hazard_function_and_cumulative_hazard_function\" rel=\"nofollow noreferrer\">Wikipedia entry</a>. What you propose is thus possible, in principle.</p>\n<p>The difficulty is that you would no longer have a proportional hazards model. Consider the hazard ratio for individuals <span class=\"math-container\">$i$</span> and <span class=\"math-container\">$j$</span> at time <span class=\"math-container\">$t$</span> in your additive proposal:</p>\n<p><span class=\"math-container\">$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}}{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}. $$</span></p>\n<p>Compare that to a model with multiplicative hazards:</p>\n<p><span class=\"math-container\">$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}} {\\lambda_1\\gamma_1 t^{\\gamma_1-1}\\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}=\\frac{U_i(t)^{\\gamma_2-1}}{U_j(t)^{\\gamma_2-1}}. $$</span></p>\n<p>Unlike your additive suggestion, with the multiplicative hazards the hazard ratio between individuals <span class=\"math-container\">$i$</span> and <span class=\"math-container\">$j$</span> is constant over time, given their values of <span class=\"math-container\">$U(t)$</span>.</p>\n<p>A proportional hazards model has a particular advantage in that it only depends on the covariate values in place at event times. Other types of models, like accelerated-failure-time models other than Weibull, depend on the entire history of time-varying covariate values like <span class=\"math-container\">$U(t)$</span>. That's one reason for favoring multiplicative hazards.</p>\n", "answer_id": 676869, "answer_text": "The only requirement for a hazard as a function of time is that it be non-negative and integrate to infinity in the limit of infinite time. See the Wikipedia entry (https://en.wikipedia.org/wiki/Survival_analysis#Hazard_function_and_cumulative_hazard_function). What you propose is thus possible, in principle.\n\n\n\n\nThe difficulty is that you would no longer have a proportional hazards model. Consider the hazard ratio for individuals $i$ and $j$ at time $t$ in your additive proposal:\n\n\n\n\n$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}}{\\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}. $$\n\n\n\n\nCompare that to a model with multiplicative hazards:\n\n\n\n\n$$\\frac{h_i}{h_j}=\\frac{\\lambda_1\\gamma_1 t^{\\gamma_1-1} \\lambda_2\\gamma_2\\, U_i(t)^{\\gamma_2-1}} {\\lambda_1\\gamma_1 t^{\\gamma_1-1}\\lambda_2\\gamma_2\\, U_j(t)^{\\gamma_2-1}}=\\frac{U_i(t)^{\\gamma_2-1}}{U_j(t)^{\\gamma_2-1}}. $$\n\n\n\n\nUnlike your additive suggestion, with the multiplicative hazards the hazard ratio between individuals $i$ and $j$ is constant over time, given their values of $U(t)$.\n\n\n\n\nA proportional hazards model has a particular advantage in that it only depends on the covariate values in place at event times. Other types of models, like accelerated-failure-time models other than Weibull, depend on the entire history of time-varying covariate values like $U(t)$. That's one reason for favoring multiplicative hazards.", "answer_url": "https://stats.stackexchange.com/a/676869", "author": "EdM", "author_url": "https://stats.stackexchange.com/users/28500/edm", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-14T15:23:45+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676850, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "EdM", "profile_url": "https://stats.stackexchange.com/users/28500/edm", "user_type": "registered"}, "created_at": "2026-08-14T15:23:45+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "3D1D9A3E-6087-4625-BCEE-EC8203D245D9", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/3D1D9A3E-6087-4625-BCEE-EC8203D245D9/view-source"}], "score": 4, "updated_at": "2026-08-14T15:23:45+00:00"}], "domain": "statistics", "external_links": ["https://en.wikipedia.org/wiki/Survival_analysis#Hazard_function_and_cumulative_hazard_function"], "medical_sensitive": false, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "adamkostanov", "question_author_url": "https://stats.stackexchange.com/users/512554/adamkostanov", "question_author_user_type": "registered", "question_created_at": "2026-08-12T05:22:15+00:00", "question_html": "<p>An object is manufactured on some date. There are two things which contribute to the risk of failure: time passed since the date of manufacture and the amount of cumulative usage.</p>\n<p>Assuming there is data on multiple objects at different time points, a parametric PH model can be used (<span class=\"math-container\">$t$</span> = time since manufacture, <span class=\"math-container\">$U(t)$</span> = cumulative usage up to time <span class=\"math-container\">$t$</span>):</p>\n<p><span class=\"math-container\">$$h(t \\mid U) = \\lambda \\gamma t^{\\gamma-1}\\,\\exp\\!\\big(\\beta_1 U(t)\\big)$$</span></p>\n<p>Is it possible to define a model where two hazard functions are added for the hazard from time and hazard from cumulative usage?</p>\n<p><span class=\"math-container\">$$h(t) = \\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U(t)^{\\gamma_2-1}$$</span></p>\n<p>Typically, I have seen additive hazard models that are additive in terms of covariates (Aalen additive hazards model):</p>\n<p><span class=\"math-container\">$$\\lambda(t \\mid X) = \\beta_0(t) + \\beta_1(t) X_1 + \\beta_2(t) X_2 + \\cdots + \\beta_p(t) X_p$$</span></p>\n<p>But is it possible to have a model that is additive in the hazard contributions of two time scales?</p>\n<p><span class=\"math-container\">$$h(t) = \\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U(t)^{\\gamma_2-1}$$</span></p>\n", "question_id": 676850, "question_license": "CC BY-SA 4.0", "question_score": 4, "question_text": "An object is manufactured on some date. There are two things which contribute to the risk of failure: time passed since the date of manufacture and the amount of cumulative usage.\n\n\n\n\nAssuming there is data on multiple objects at different time points, a parametric PH model can be used ($t$ = time since manufacture, $U(t)$ = cumulative usage up to time $t$):\n\n\n\n\n$$h(t \\mid U) = \\lambda \\gamma t^{\\gamma-1}\\,\\exp\\!\\big(\\beta_1 U(t)\\big)$$\n\n\n\n\nIs it possible to define a model where two hazard functions are added for the hazard from time and hazard from cumulative usage?\n\n\n\n\n$$h(t) = \\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U(t)^{\\gamma_2-1}$$\n\n\n\n\nTypically, I have seen additive hazard models that are additive in terms of covariates (Aalen additive hazards model):\n\n\n\n\n$$\\lambda(t \\mid X) = \\beta_0(t) + \\beta_1(t) X_1 + \\beta_2(t) X_2 + \\cdots + \\beta_p(t) X_p$$\n\n\n\n\nBut is it possible to have a model that is additive in the hazard contributions of two time scales?\n\n\n\n\n$$h(t) = \\lambda_1\\gamma_1 t^{\\gamma_1-1} + \\lambda_2\\gamma_2\\, U(t)^{\\gamma_2-1}$$", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "adamkostanov", "profile_url": "https://stats.stackexchange.com/users/512554/adamkostanov", "user_type": "registered"}, "created_at": "2026-08-12T05:22:15+00:00", "raw_file": "raw/codex_api_v1/4f74c7234d8d68222a97a637c85bc14d5aa045a3d3f336d95a6178b29bc656cd_1790825248437413600_0.json", "raw_sha256": "8385102f4bd0517d9240ab820144fa12ccb111d0c4dd000e66bdc0e0ba0294fe", "revision_guid": "D26D0E7A-7AF6-4114-984B-40639054B7E2", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/D26D0E7A-7AF6-4114-984B-40639054B7E2/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/676850/how-to-let-different-factors-influence-survival-hazard-within-a-model", "split": "validation", "split_group": "a5373623168c1c550833ba1b84284085b1884c554ada82546c601a9dd3638f4b", "tags": ["survival"], "thread_id": "stats:676850", "title": "How to let different factors influence survival hazard within a model?"}}
{"accepted_answer": null, "other_answers": [{"answer_html": "<p>EdM’s answer is very good but a couple of additional points:</p>\n<ol>\n<li>LASSO and ElasticNet <em>can</em> be thought of as a kind of feature selection, but it is better to think of them as forms of regularization. It just so happens that when you constrain optimization using the <span class=\"math-container\">$L^1$</span> norm, that you will get a sparse solution. This is only true of LASSO since ElasticNet allows for constraints using <span class=\"math-container\">$L^1$</span> and <span class=\"math-container\">$L^2$</span> norms. The latter norm, by itself, will not generally lead to a sparse coefficient vector.</li>\n<li>You might use both PCA and a regularized optimization routine if, for example, you have a large number of numeric features (say pixel intensities) that you’d first like to project onto a lower dimensional space (e.g. using the first <span class=\"math-container\">$k$</span> principal components). Some might call that feature engineering. Then, you might also have, for each example, some number of other features / variables about the image that help with whatever your problem is (e.g. classification). So you’d first use PCA on the subset of features that need to be projected down to a manageable / useful set, and then use those together with the remaining features in the actual learning process.</li>\n<li>As Dave mentions in his comment, there aren’t really any standards here other than thinking about what might work best for your problem. Having hundreds or thousands of pixel intensities might not be as optimal when using LASSO as having the first <span class=\"math-container\">$k$</span> principal components plus the remaining, possibly categorical variables.</li>\n</ol>\n", "answer_id": 676882, "answer_text": "EdM’s answer is very good but a couple of additional points:\n\n\n\n\n\nLASSO and ElasticNet can be thought of as a kind of feature selection, but it is better to think of them as forms of regularization. It just so happens that when you constrain optimization using the $L^1$ norm, that you will get a sparse solution. This is only true of LASSO since ElasticNet allows for constraints using $L^1$ and $L^2$ norms. The latter norm, by itself, will not generally lead to a sparse coefficient vector.\n\n\n\n\nYou might use both PCA and a regularized optimization routine if, for example, you have a large number of numeric features (say pixel intensities) that you’d first like to project onto a lower dimensional space (e.g. using the first $k$ principal components). Some might call that feature engineering. Then, you might also have, for each example, some number of other features / variables about the image that help with whatever your problem is (e.g. classification). So you’d first use PCA on the subset of features that need to be projected down to a manageable / useful set, and then use those together with the remaining features in the actual learning process.\n\n\n\n\nAs Dave mentions in his comment, there aren’t really any standards here other than thinking about what might work best for your problem. Having hundreds or thousands of pixel intensities might not be as optimal when using LASSO as having the first $k$ principal components plus the remaining, possibly categorical variables.", "answer_url": "https://stats.stackexchange.com/a/676882", "author": "Rick Hass", "author_url": "https://stats.stackexchange.com/users/97844/rick-hass", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-15T18:50:47+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676873, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Rick Hass", "profile_url": "https://stats.stackexchange.com/users/97844/rick-hass", "user_type": "registered"}, "created_at": "2026-08-15T18:50:47+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "20D33DFC-5C33-4102-8CB3-EEFE63F27B52", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/20D33DFC-5C33-4102-8CB3-EEFE63F27B52/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Rick Hass", "profile_url": "https://stats.stackexchange.com/users/97844/rick-hass", "user_type": "registered"}, "created_at": "2026-08-16T00:28:16+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "15E83EAD-6155-4905-A2FC-ED94BFB7DC67", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/15E83EAD-6155-4905-A2FC-ED94BFB7DC67/view-source"}], "score": 3, "updated_at": "2026-08-16T00:28:16+00:00"}], "product": "statistics_qa", "question": "I understand that PCA is for eliminating collinearity and reducing dimensions while the other is used for feature selection. I was wondering if there is any way to use these two methods together for an example if you wanted do feature selection but also wanted to get rid of collinearity which elastic net/lasso does poorly.\n\n\n\n\nIn what cases would you use these two methods together and what cases would you use them separately? What is the general standard of practice is it to do it together or separate?", "record_id": "Scientific-Statistics-QA:stats:676873", "selected_answer": {"answer_html": "<p>Performing LASSO on standard principal components or following LASSO with PCA might still end up with all retained predictors contributing to each component.</p>\n<p>An alternative is <a href=\"https://en.wikipedia.org/wiki/Sparse_PCA\" rel=\"noreferrer\">sparse principal component analysis</a> (SPCA). It uses a LASSO-type penalty to cut down on the number of original variables contributing to each component. Quoting:</p>\n<blockquote>\n<p>A particular disadvantage of ordinary PCA is that the principal components are usually linear combinations of all input variables. SPCA overcomes this disadvantage by finding components that are linear combinations of just a few input variables (SPCs).</p>\n</blockquote>\n<p>Frank Harrell recommends it as one way to perform &quot;data reduction&quot; on predictors without using the outcome. See <a href=\"https://hbiostat.org/rmsc/impred.html#sec-impred-sparsepc\" rel=\"noreferrer\">Section 8.6.1 of his Regression Modeling Strategies</a>. Both the above references have links to implementations in R. For example, the R <a href=\"https://cran.r-project.org/package=elasticnet\" rel=\"noreferrer\"><code>elasticnet</code> package</a> has one implementation in its <code>spca()</code> function.</p>\n", "answer_id": 676880, "answer_text": "Performing LASSO on standard principal components or following LASSO with PCA might still end up with all retained predictors contributing to each component.\n\n\n\n\nAn alternative is sparse principal component analysis (https://en.wikipedia.org/wiki/Sparse_PCA) (SPCA). It uses a LASSO-type penalty to cut down on the number of original variables contributing to each component. Quoting:\n\n\n\n\n\n\n\nA particular disadvantage of ordinary PCA is that the principal components are usually linear combinations of all input variables. SPCA overcomes this disadvantage by finding components that are linear combinations of just a few input variables (SPCs).\n\n\n\n\n\n\n\nFrank Harrell recommends it as one way to perform \"data reduction\" on predictors without using the outcome. See Section 8.6.1 of his Regression Modeling Strategies (https://hbiostat.org/rmsc/impred.html#sec-impred-sparsepc). Both the above references have links to implementations in R. For example, the R elasticnet package (https://cran.r-project.org/package=elasticnet) has one implementation in its spca() function.", "answer_url": "https://stats.stackexchange.com/a/676880", "author": "EdM", "author_url": "https://stats.stackexchange.com/users/28500/edm", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-15T13:34:16+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676873, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "EdM", "profile_url": "https://stats.stackexchange.com/users/28500/edm", "user_type": "registered"}, "created_at": "2026-08-15T13:34:16+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "2DE20E9C-6EC1-4527-BC84-458EE2BCF893", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/2DE20E9C-6EC1-4527-BC84-458EE2BCF893/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "EdM", "profile_url": "https://stats.stackexchange.com/users/28500/edm", "user_type": "registered"}, "created_at": "2026-08-15T13:44:54+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "F6AC7515-6D89-427A-BA3B-ACAE39221913", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/F6AC7515-6D89-427A-BA3B-ACAE39221913/view-source"}], "score": 7, "updated_at": "2026-08-15T13:44:54+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": null, "answers": [{"answer_html": "<p>Performing LASSO on standard principal components or following LASSO with PCA might still end up with all retained predictors contributing to each component.</p>\n<p>An alternative is <a href=\"https://en.wikipedia.org/wiki/Sparse_PCA\" rel=\"noreferrer\">sparse principal component analysis</a> (SPCA). It uses a LASSO-type penalty to cut down on the number of original variables contributing to each component. Quoting:</p>\n<blockquote>\n<p>A particular disadvantage of ordinary PCA is that the principal components are usually linear combinations of all input variables. SPCA overcomes this disadvantage by finding components that are linear combinations of just a few input variables (SPCs).</p>\n</blockquote>\n<p>Frank Harrell recommends it as one way to perform &quot;data reduction&quot; on predictors without using the outcome. See <a href=\"https://hbiostat.org/rmsc/impred.html#sec-impred-sparsepc\" rel=\"noreferrer\">Section 8.6.1 of his Regression Modeling Strategies</a>. Both the above references have links to implementations in R. For example, the R <a href=\"https://cran.r-project.org/package=elasticnet\" rel=\"noreferrer\"><code>elasticnet</code> package</a> has one implementation in its <code>spca()</code> function.</p>\n", "answer_id": 676880, "answer_text": "Performing LASSO on standard principal components or following LASSO with PCA might still end up with all retained predictors contributing to each component.\n\n\n\n\nAn alternative is sparse principal component analysis (https://en.wikipedia.org/wiki/Sparse_PCA) (SPCA). It uses a LASSO-type penalty to cut down on the number of original variables contributing to each component. Quoting:\n\n\n\n\n\n\n\nA particular disadvantage of ordinary PCA is that the principal components are usually linear combinations of all input variables. SPCA overcomes this disadvantage by finding components that are linear combinations of just a few input variables (SPCs).\n\n\n\n\n\n\n\nFrank Harrell recommends it as one way to perform \"data reduction\" on predictors without using the outcome. See Section 8.6.1 of his Regression Modeling Strategies (https://hbiostat.org/rmsc/impred.html#sec-impred-sparsepc). Both the above references have links to implementations in R. For example, the R elasticnet package (https://cran.r-project.org/package=elasticnet) has one implementation in its spca() function.", "answer_url": "https://stats.stackexchange.com/a/676880", "author": "EdM", "author_url": "https://stats.stackexchange.com/users/28500/edm", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-15T13:34:16+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676873, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "EdM", "profile_url": "https://stats.stackexchange.com/users/28500/edm", "user_type": "registered"}, "created_at": "2026-08-15T13:34:16+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "2DE20E9C-6EC1-4527-BC84-458EE2BCF893", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/2DE20E9C-6EC1-4527-BC84-458EE2BCF893/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "EdM", "profile_url": "https://stats.stackexchange.com/users/28500/edm", "user_type": "registered"}, "created_at": "2026-08-15T13:44:54+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "F6AC7515-6D89-427A-BA3B-ACAE39221913", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/F6AC7515-6D89-427A-BA3B-ACAE39221913/view-source"}], "score": 7, "updated_at": "2026-08-15T13:44:54+00:00"}, {"answer_html": "<p>EdM’s answer is very good but a couple of additional points:</p>\n<ol>\n<li>LASSO and ElasticNet <em>can</em> be thought of as a kind of feature selection, but it is better to think of them as forms of regularization. It just so happens that when you constrain optimization using the <span class=\"math-container\">$L^1$</span> norm, that you will get a sparse solution. This is only true of LASSO since ElasticNet allows for constraints using <span class=\"math-container\">$L^1$</span> and <span class=\"math-container\">$L^2$</span> norms. The latter norm, by itself, will not generally lead to a sparse coefficient vector.</li>\n<li>You might use both PCA and a regularized optimization routine if, for example, you have a large number of numeric features (say pixel intensities) that you’d first like to project onto a lower dimensional space (e.g. using the first <span class=\"math-container\">$k$</span> principal components). Some might call that feature engineering. Then, you might also have, for each example, some number of other features / variables about the image that help with whatever your problem is (e.g. classification). So you’d first use PCA on the subset of features that need to be projected down to a manageable / useful set, and then use those together with the remaining features in the actual learning process.</li>\n<li>As Dave mentions in his comment, there aren’t really any standards here other than thinking about what might work best for your problem. Having hundreds or thousands of pixel intensities might not be as optimal when using LASSO as having the first <span class=\"math-container\">$k$</span> principal components plus the remaining, possibly categorical variables.</li>\n</ol>\n", "answer_id": 676882, "answer_text": "EdM’s answer is very good but a couple of additional points:\n\n\n\n\n\nLASSO and ElasticNet can be thought of as a kind of feature selection, but it is better to think of them as forms of regularization. It just so happens that when you constrain optimization using the $L^1$ norm, that you will get a sparse solution. This is only true of LASSO since ElasticNet allows for constraints using $L^1$ and $L^2$ norms. The latter norm, by itself, will not generally lead to a sparse coefficient vector.\n\n\n\n\nYou might use both PCA and a regularized optimization routine if, for example, you have a large number of numeric features (say pixel intensities) that you’d first like to project onto a lower dimensional space (e.g. using the first $k$ principal components). Some might call that feature engineering. Then, you might also have, for each example, some number of other features / variables about the image that help with whatever your problem is (e.g. classification). So you’d first use PCA on the subset of features that need to be projected down to a manageable / useful set, and then use those together with the remaining features in the actual learning process.\n\n\n\n\nAs Dave mentions in his comment, there aren’t really any standards here other than thinking about what might work best for your problem. Having hundreds or thousands of pixel intensities might not be as optimal when using LASSO as having the first $k$ principal components plus the remaining, possibly categorical variables.", "answer_url": "https://stats.stackexchange.com/a/676882", "author": "Rick Hass", "author_url": "https://stats.stackexchange.com/users/97844/rick-hass", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-15T18:50:47+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676873, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Rick Hass", "profile_url": "https://stats.stackexchange.com/users/97844/rick-hass", "user_type": "registered"}, "created_at": "2026-08-15T18:50:47+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "20D33DFC-5C33-4102-8CB3-EEFE63F27B52", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/20D33DFC-5C33-4102-8CB3-EEFE63F27B52/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Rick Hass", "profile_url": "https://stats.stackexchange.com/users/97844/rick-hass", "user_type": "registered"}, "created_at": "2026-08-16T00:28:16+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "15E83EAD-6155-4905-A2FC-ED94BFB7DC67", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/15E83EAD-6155-4905-A2FC-ED94BFB7DC67/view-source"}], "score": 3, "updated_at": "2026-08-16T00:28:16+00:00"}], "domain": "statistics", "external_links": ["https://cran.r-project.org/package=elasticnet", "https://en.wikipedia.org/wiki/Sparse_PCA", "https://hbiostat.org/rmsc/impred.html#sec-impred-sparsepc"], "medical_sensitive": false, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "Karl Suh", "question_author_url": "https://stats.stackexchange.com/users/513622/karl-suh", "question_author_user_type": "registered", "question_created_at": "2026-08-15T03:42:38+00:00", "question_html": "<p>I understand that PCA is for eliminating collinearity and reducing dimensions while the other is used for feature selection. I was wondering if there is any way to use these two methods together for an example if you wanted do feature selection but also wanted to get rid of collinearity which elastic net/lasso does poorly.</p>\n<p>In what cases would you use these two methods together and what cases would you use them separately? What is the general standard of practice is it to do it together or separate?</p>\n", "question_id": 676873, "question_license": "CC BY-SA 4.0", "question_score": 5, "question_text": "I understand that PCA is for eliminating collinearity and reducing dimensions while the other is used for feature selection. I was wondering if there is any way to use these two methods together for an example if you wanted do feature selection but also wanted to get rid of collinearity which elastic net/lasso does poorly.\n\n\n\n\nIn what cases would you use these two methods together and what cases would you use them separately? What is the general standard of practice is it to do it together or separate?", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Karl Suh", "profile_url": "https://stats.stackexchange.com/users/513622/karl-suh", "user_type": "registered"}, "created_at": "2026-08-15T03:42:38+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "C888628D-3C97-4B17-86E4-AF7475A3C710", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/C888628D-3C97-4B17-86E4-AF7475A3C710/view-source"}, {"content_license": null, "contributor": {"display_name": "[deleted/unavailable user]", "profile_url": null, "user_type": "does_not_exist"}, "created_at": "2026-08-15T13:55:27+00:00", "raw_file": "raw/codex_api_v1/01ff643b5e725b2402c38de261074c3bef3326820f24db4342e17e318353268e_1790825246205492400_0.json", "raw_sha256": "a0521ae83c8630be00a12499e9aaa253069e57a5e0ff902fea3e9b8e7acb810f", "revision_guid": "1513EDD5-94FC-4B55-A64B-81770DFE0CB8", "revision_number": null, "revision_type": "vote_based", "revision_url": "https://stats.stackexchange.com/revisions/1513EDD5-94FC-4B55-A64B-81770DFE0CB8/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/676873/can-you-use-pca-and-elastic-net-lasso-together-or-do-you-have-to-use-one-or-the", "split": "validation", "split_group": "b0befa11d334e3ea0cb0f6c32cda5c87da78493a28ed637e497c3e4ae2e250e6", "tags": ["pca", "lasso", "elastic-net"], "thread_id": "stats:676873", "title": "Can you use PCA and Elastic Net/Lasso together or do you have to use one or the other? Are there any use cases for using these two methods together?"}}
{"accepted_answer": {"answer_html": "<p>Rather than fitting separate models, you can handle both age groups in a single <code>gls()</code> model with <code>age</code> interactions. This has two advantages over separate <code>auto.arima()</code> models: you get formal tests of whether intervention effects differ between groups via the interaction terms, and you can account for the fact that the two series have very different variances.</p>\n<p><code>age * (time + step1 + ramp1 + ...)</code> gives each group its own intercept, pre-intervention slope, and intervention effects, plus tests of whether they differ. Then, <code>corAR1(form = ~ 1 | age)</code> fits AR(1) autocorrelation within each group and <code>varIdent(form = ~ 1 | age)</code> allows different residual variances per group, which matters here since children's prevalence is an order of magnitude larger.</p>\n<pre class=\"lang-r prettyprint-override\"><code>library(nlme)\nlibrary(dplyr)\n\ninterventions &lt;- as.Date(c(&quot;2021-09-13&quot;, &quot;2022-06-02&quot;, &quot;2023-06-29&quot;))\n\nd_its &lt;- d |&gt;\n arrange(age, month) |&gt;\n mutate(.by = age,\n time = row_number() - 1,\n step1 = as.numeric(month &gt;= interventions[1]),\n ramp1 = ifelse(step1 == 1, cumsum(step1) - 1, 0),\n step2 = as.numeric(month &gt;= interventions[2]),\n ramp2 = ifelse(step2 == 1, cumsum(step2) - 1, 0),\n step3 = as.numeric(month &gt;= interventions[3]),\n ramp3 = ifelse(step3 == 1, cumsum(step3) - 1, 0),\n sin12 = sin(2 * pi * time / 12),\n cos12 = cos(2 * pi * time / 12),\n sin6 = sin(2 * pi * time / 6),\n cos6 = cos(2 * pi * time / 6),\n sin4 = sin(2 * pi * time / 4),\n cos4 = cos(2 * pi * time / 4)\n ) |&gt;\n mutate(age = factor(age))\n\nfit &lt;- gls(\n prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 +\n sin12 + cos12 + sin6 + cos6 + sin4 + cos4),\n data = d_its,\n correlation = corAR1(form = ~ 1 | age),\n weights = varIdent(form = ~ 1 | age)\n)\n</code></pre>\n<p>The Fourier terms (<code>sin12</code>, <code>cos12</code>, etc.) handle the recurring seasonal pattern, which is more stable than trying to absorb it through ARMA error terms (see <a href=\"https://otexts.com/fpp3/dhr.html\" rel=\"nofollow noreferrer\">https://otexts.com/fpp3/dhr.html</a>).</p>\n<p>The reference level is Adults, so coefficients are interpreted as:</p>\n<ul>\n<li><code>time</code>: pre-intervention slope for adults (prevalence per month)</li>\n<li><code>ageChildren:time</code>: <em>additional</em> slope for children;</li>\n</ul>\n<p>The same logic applies for all <code>ramp</code> and <code>step</code> terms.</p>\n<pre class=\"lang-r prettyprint-override\"><code>coef(fit) |&gt;\n (\\(e) data.frame(\n Segment = c(&quot;Pre&quot;, &quot;Post-1&quot;, &quot;Post-2&quot;, &quot;Post-3&quot;),\n Adults = round(cumsum(c(e[&quot;time&quot;],\n e[&quot;ramp1&quot;],\n e[&quot;ramp2&quot;],\n e[&quot;ramp3&quot;])), 3),\n Children = round(cumsum(c(e[&quot;time&quot;] + e[&quot;ageChildren:time&quot;],\n e[&quot;ramp1&quot;] + e[&quot;ageChildren:ramp1&quot;],\n e[&quot;ramp2&quot;] + e[&quot;ageChildren:ramp2&quot;],\n e[&quot;ramp3&quot;] + e[&quot;ageChildren:ramp3&quot;])), 3)\n))()\n\n#&gt; Segment Adults Children\n#&gt; time Pre 0.152 1.445\n#&gt; ramp1 Post-1 0.919 9.276\n#&gt; ramp2 Post-2 1.265 6.667\n#&gt; ramp3 Post-3 2.387 8.418\n</code></pre>\n<pre class=\"lang-r prettyprint-override\"><code>par(mfrow = c(1, 2))\nacf(residuals(fit)[d_its<span class=\"math-container\">$age == \"Adults\"], main = \"Adults\")\nacf(residuals(fit)[d_its$</span>age == &quot;Children&quot;], main = &quot;Children&quot;)\ndev.off()\n</code></pre>\n<p><img src=\"https://i.sstatic.net/2fnDCjiM.png\" alt=\"\" /></p>\n<pre class=\"lang-r prettyprint-override\"><code>summary(fit)\n\n#&gt; Generalized least squares fit by REML\n#&gt; Model: prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 + sin12 + cos12 + sin6 + cos6 + sin4 + cos4) \n#&gt; Data: d_its \n#&gt; AIC BIC logLik\n#&gt; 1665.172 1769.226 -801.5858\n#&gt; \n#&gt; Correlation Structure: AR(1)\n#&gt; Formula: ~1 | age \n#&gt; Parameter estimate(s):\n#&gt; Phi \n#&gt; 0.1231768 \n#&gt; Variance function:\n#&gt; Structure: Different standard deviations per stratum\n#&gt; Formula: ~1 | age \n#&gt; Parameter estimates:\n#&gt; Adults Children \n#&gt; 1.000000 9.435418 \n#&gt; \n#&gt; Coefficients:\n#&gt; Value Std.Error t-value p-value\n#&gt; (Intercept) 10.25450 0.701853 14.610597 0.0000\n#&gt; ageChildren 177.16318 6.659369 26.603599 0.0000\n#&gt; time 0.15162 0.017799 8.518225 0.0000\n#&gt; step1 1.83019 1.934456 0.946100 0.3452\n#&gt; ramp1 0.76689 0.379858 2.018892 0.0448\n#&gt; step2 -0.98456 2.681497 -0.367167 0.7139\n#&gt; ramp2 0.34620 0.449034 0.770992 0.4416\n#&gt; step3 -4.38832 2.094825 -2.094838 0.0374\n#&gt; ramp3 1.12257 0.257109 4.366133 0.0000\n#&gt; sin12 0.01845 0.399784 0.046145 0.9632\n#&gt; cos12 1.16269 0.380402 3.056467 0.0025\n#&gt; sin6 -0.70555 0.358820 -1.966309 0.0506\n#&gt; cos6 -0.97079 0.355213 -2.732980 0.0068\n#&gt; sin4 0.78337 0.333359 2.349941 0.0197\n#&gt; cos4 0.05479 0.333865 0.164117 0.8698\n#&gt; ageChildren:time 1.29381 0.168883 7.661002 0.0000\n#&gt; ageChildren:step1 -1.02662 18.354630 -0.055933 0.9554\n#&gt; ageChildren:ramp1 7.06407 3.604192 1.959960 0.0513\n#&gt; ageChildren:step2 -35.82645 25.442751 -1.408120 0.1606\n#&gt; ageChildren:ramp2 -2.95530 4.260553 -0.693643 0.4887\n#&gt; ageChildren:step3 -29.49930 19.876247 -1.484148 0.1393\n#&gt; ageChildren:ramp3 0.62770 2.439516 0.257306 0.7972\n#&gt; ageChildren:sin12 4.22521 3.793256 1.113874 0.2666\n#&gt; ageChildren:cos12 36.54005 3.609352 10.123715 0.0000\n#&gt; ageChildren:sin6 -9.76953 3.404578 -2.869528 0.0045\n#&gt; ageChildren:cos6 -25.08251 3.370352 -7.442104 0.0000\n#&gt; ageChildren:sin4 13.68702 3.162997 4.327230 0.0000\n#&gt; ageChildren:cos4 14.66062 3.167797 4.628016 0.0000\n#&gt; \n#&gt; Correlation: \n#&gt; (Intr) agChld time step1 ramp1 step2 ramp2 step3 \n#&gt; ageChildren -0.105 \n#&gt; time -0.862 0.091 \n#&gt; step1 0.173 -0.018 -0.309 \n#&gt; ramp1 0.057 -0.006 -0.063 -0.763 \n#&gt; step2 -0.021 0.002 0.020 0.366 -0.711 \n#&gt; ramp2 -0.004 0.000 0.005 0.655 -0.831 0.306 \n#&gt; step3 -0.015 0.002 0.015 0.007 -0.031 0.300 -0.403 \n#&gt; ramp3 -0.016 0.002 0.016 0.008 -0.023 0.514 -0.520 0.649\n#&gt; sin12 -0.056 0.006 0.043 0.155 -0.247 0.317 0.074 0.209\n#&gt; cos12 -0.050 0.005 0.069 -0.188 0.137 -0.028 -0.155 0.065\n#&gt; sin6 -0.008 0.001 -0.006 0.034 0.018 -0.103 0.052 -0.126\n#&gt; cos6 -0.010 0.001 0.002 0.047 -0.026 -0.035 0.060 -0.071\n#&gt; sin4 -0.024 0.002 0.028 -0.033 -0.004 0.059 -0.040 0.066\n#&gt; cos4 -0.011 0.001 0.002 0.063 -0.078 0.095 0.024 0.069\n#&gt; ageChildren:time 0.091 -0.862 -0.105 0.033 0.007 -0.002 0.000 -0.002\n#&gt; ageChildren:step1 -0.018 0.173 0.033 -0.105 0.080 -0.039 -0.069 -0.001\n#&gt; ageChildren:ramp1 -0.006 0.057 0.007 0.080 -0.105 0.075 0.088 0.003\n#&gt; ageChildren:step2 0.002 -0.021 -0.002 -0.039 0.075 -0.105 -0.032 -0.032\n#&gt; ageChildren:ramp2 0.000 -0.004 0.000 -0.069 0.088 -0.032 -0.105 0.043\n#&gt; ageChildren:step3 0.002 -0.015 -0.002 -0.001 0.003 -0.032 0.043 -0.105\n#&gt; ageChildren:ramp3 0.002 -0.016 -0.002 -0.001 0.002 -0.054 0.055 -0.068\n#&gt; ageChildren:sin12 0.006 -0.056 -0.005 -0.016 0.026 -0.033 -0.008 -0.022\n#&gt; ageChildren:cos12 0.005 -0.050 -0.007 0.020 -0.014 0.003 0.016 -0.007\n#&gt; ageChildren:sin6 0.001 -0.008 0.001 -0.004 -0.002 0.011 -0.005 0.013\n#&gt; ageChildren:cos6 0.001 -0.010 0.000 -0.005 0.003 0.004 -0.006 0.007\n#&gt; ageChildren:sin4 0.002 -0.024 -0.003 0.003 0.000 -0.006 0.004 -0.007\n#&gt; ageChildren:cos4 0.001 -0.011 0.000 -0.007 0.008 -0.010 -0.002 -0.007\n#&gt; ramp3 sin12 cos12 sin6 cos6 sin4 cos4 agChl:\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 0.235 \n#&gt; cos12 0.057 -0.008 \n#&gt; sin6 -0.099 -0.036 -0.015 \n#&gt; cos6 -0.058 -0.011 -0.017 0.017 \n#&gt; sin4 0.074 0.025 0.014 -0.013 -0.006 \n#&gt; cos4 0.073 0.041 -0.007 -0.009 -0.004 0.009 \n#&gt; ageChildren:time -0.002 -0.005 -0.007 0.001 0.000 -0.003 0.000 \n#&gt; ageChildren:step1 -0.001 -0.016 0.020 -0.004 -0.005 0.003 -0.007 -0.309\n#&gt; ageChildren:ramp1 0.002 0.026 -0.014 -0.002 0.003 0.000 0.008 -0.063\n#&gt; ageChildren:step2 -0.054 -0.033 0.003 0.011 0.004 -0.006 -0.010 0.020\n#&gt; ageChildren:ramp2 0.055 -0.008 0.016 -0.005 -0.006 0.004 -0.002 0.005\n#&gt; ageChildren:step3 -0.068 -0.022 -0.007 0.013 0.007 -0.007 -0.007 0.015\n#&gt; ageChildren:ramp3 -0.105 -0.025 -0.006 0.010 0.006 -0.008 -0.008 0.016\n#&gt; ageChildren:sin12 -0.025 -0.105 0.001 0.004 0.001 -0.003 -0.004 0.043\n#&gt; ageChildren:cos12 -0.006 0.001 -0.105 0.002 0.002 -0.002 0.001 0.069\n#&gt; ageChildren:sin6 0.010 0.004 0.002 -0.105 -0.002 0.001 0.001 -0.006\n#&gt; ageChildren:cos6 0.006 0.001 0.002 -0.002 -0.105 0.001 0.000 0.002\n#&gt; ageChildren:sin4 -0.008 -0.003 -0.002 0.001 0.001 -0.105 -0.001 0.028\n#&gt; ageChildren:cos4 -0.008 -0.004 0.001 0.001 0.000 -0.001 -0.105 0.002\n#&gt; agChldrn:s1 agChldrn:r1 agChldrn:s2 agChldrn:r2 agChldrn:s3\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 -0.763 \n#&gt; ageChildren:step2 0.366 -0.711 \n#&gt; ageChildren:ramp2 0.655 -0.831 0.306 \n#&gt; ageChildren:step3 0.007 -0.031 0.300 -0.403 \n#&gt; ageChildren:ramp3 0.008 -0.023 0.514 -0.520 0.649 \n#&gt; ageChildren:sin12 0.155 -0.247 0.317 0.074 0.209 \n#&gt; ageChildren:cos12 -0.188 0.137 -0.028 -0.155 0.065 \n#&gt; ageChildren:sin6 0.034 0.018 -0.103 0.052 -0.126 \n#&gt; ageChildren:cos6 0.047 -0.026 -0.035 0.060 -0.071 \n#&gt; ageChildren:sin4 -0.033 -0.004 0.059 -0.040 0.066 \n#&gt; ageChildren:cos4 0.063 -0.078 0.095 0.024 0.069 \n#&gt; agChldrn:r3 agChldrn:s12 agChldrn:c12 agChldrn:s6 agChldrn:c6\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 \n#&gt; ageChildren:step2 \n#&gt; ageChildren:ramp2 \n#&gt; ageChildren:step3 \n#&gt; ageChildren:ramp3 \n#&gt; ageChildren:sin12 0.235 \n#&gt; ageChildren:cos12 0.057 -0.008 \n#&gt; ageChildren:sin6 -0.099 -0.036 -0.015 \n#&gt; ageChildren:cos6 -0.058 -0.011 -0.017 0.017 \n#&gt; ageChildren:sin4 0.074 0.025 0.014 -0.013 -0.006 \n#&gt; ageChildren:cos4 0.073 0.041 -0.007 -0.009 -0.004 \n#&gt; agChldrn:s4\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 \n#&gt; ageChildren:step2 \n#&gt; ageChildren:ramp2 \n#&gt; ageChildren:step3 \n#&gt; ageChildren:ramp3 \n#&gt; ageChildren:sin12 \n#&gt; ageChildren:cos12 \n#&gt; ageChildren:sin6 \n#&gt; ageChildren:cos6 \n#&gt; ageChildren:sin4 \n#&gt; ageChildren:cos4 0.009 \n#&gt; \n#&gt; Standardized residuals:\n#&gt; Min Q1 Med Q3 Max \n#&gt; -5.25291094 -0.42240774 0.04331099 0.45224365 3.26323939 \n#&gt; \n#&gt; Residual standard error: 2.608572 \n#&gt; Degrees of freedom: 240 total; 212 residual\n</code></pre>\n<p><sup>Created on 2026-08-29 with <a href=\"https://reprex.tidyverse.org\" rel=\"nofollow noreferrer\">reprex v2.1.1</a></sup></p>\n", "answer_id": 676996, "answer_text": "Rather than fitting separate models, you can handle both age groups in a single gls() model with age interactions. This has two advantages over separate auto.arima() models: you get formal tests of whether intervention effects differ between groups via the interaction terms, and you can account for the fact that the two series have very different variances.\n\n\n\n\nage * (time + step1 + ramp1 + ...) gives each group its own intercept, pre-intervention slope, and intervention effects, plus tests of whether they differ. Then, corAR1(form = ~ 1 | age) fits AR(1) autocorrelation within each group and varIdent(form = ~ 1 | age) allows different residual variances per group, which matters here since children's prevalence is an order of magnitude larger.\n\n\n\n\nlibrary(nlme)\nlibrary(dplyr)\n\ninterventions <- as.Date(c(\"2021-09-13\", \"2022-06-02\", \"2023-06-29\"))\n\nd_its <- d |>\n arrange(age, month) |>\n mutate(.by = age,\n time = row_number() - 1,\n step1 = as.numeric(month >= interventions[1]),\n ramp1 = ifelse(step1 == 1, cumsum(step1) - 1, 0),\n step2 = as.numeric(month >= interventions[2]),\n ramp2 = ifelse(step2 == 1, cumsum(step2) - 1, 0),\n step3 = as.numeric(month >= interventions[3]),\n ramp3 = ifelse(step3 == 1, cumsum(step3) - 1, 0),\n sin12 = sin(2 * pi * time / 12),\n cos12 = cos(2 * pi * time / 12),\n sin6 = sin(2 * pi * time / 6),\n cos6 = cos(2 * pi * time / 6),\n sin4 = sin(2 * pi * time / 4),\n cos4 = cos(2 * pi * time / 4)\n ) |>\n mutate(age = factor(age))\n\nfit <- gls(\n prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 +\n sin12 + cos12 + sin6 + cos6 + sin4 + cos4),\n data = d_its,\n correlation = corAR1(form = ~ 1 | age),\n weights = varIdent(form = ~ 1 | age)\n)\n\n\n\n\n\nThe Fourier terms (sin12, cos12, etc.) handle the recurring seasonal pattern, which is more stable than trying to absorb it through ARMA error terms (see https://otexts.com/fpp3/dhr.html (https://otexts.com/fpp3/dhr.html)).\n\n\n\n\nThe reference level is Adults, so coefficients are interpreted as:\n\n\n\n\n\ntime: pre-intervention slope for adults (prevalence per month)\n\n\n\n\nageChildren:time: additional slope for children;\n\n\n\n\n\nThe same logic applies for all ramp and step terms.\n\n\n\n\ncoef(fit) |>\n (\\(e) data.frame(\n Segment = c(\"Pre\", \"Post-1\", \"Post-2\", \"Post-3\"),\n Adults = round(cumsum(c(e[\"time\"],\n e[\"ramp1\"],\n e[\"ramp2\"],\n e[\"ramp3\"])), 3),\n Children = round(cumsum(c(e[\"time\"] + e[\"ageChildren:time\"],\n e[\"ramp1\"] + e[\"ageChildren:ramp1\"],\n e[\"ramp2\"] + e[\"ageChildren:ramp2\"],\n e[\"ramp3\"] + e[\"ageChildren:ramp3\"])), 3)\n))()\n\n#> Segment Adults Children\n#> time Pre 0.152 1.445\n#> ramp1 Post-1 0.919 9.276\n#> ramp2 Post-2 1.265 6.667\n#> ramp3 Post-3 2.387 8.418\n\n\n\n\n\npar(mfrow = c(1, 2))\nacf(residuals(fit)[d_its$age == \"Adults\"], main = \"Adults\")\nacf(residuals(fit)[d_its$age == \"Children\"], main = \"Children\")\ndev.off()\n\n\n\n\n\n[image: ; source: https://i.sstatic.net/2fnDCjiM.png]\n\n\n\n\nsummary(fit)\n\n#> Generalized least squares fit by REML\n#> Model: prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 + sin12 + cos12 + sin6 + cos6 + sin4 + cos4) \n#> Data: d_its \n#> AIC BIC logLik\n#> 1665.172 1769.226 -801.5858\n#> \n#> Correlation Structure: AR(1)\n#> Formula: ~1 | age \n#> Parameter estimate(s):\n#> Phi \n#> 0.1231768 \n#> Variance function:\n#> Structure: Different standard deviations per stratum\n#> Formula: ~1 | age \n#> Parameter estimates:\n#> Adults Children \n#> 1.000000 9.435418 \n#> \n#> Coefficients:\n#> Value Std.Error t-value p-value\n#> (Intercept) 10.25450 0.701853 14.610597 0.0000\n#> ageChildren 177.16318 6.659369 26.603599 0.0000\n#> time 0.15162 0.017799 8.518225 0.0000\n#> step1 1.83019 1.934456 0.946100 0.3452\n#> ramp1 0.76689 0.379858 2.018892 0.0448\n#> step2 -0.98456 2.681497 -0.367167 0.7139\n#> ramp2 0.34620 0.449034 0.770992 0.4416\n#> step3 -4.38832 2.094825 -2.094838 0.0374\n#> ramp3 1.12257 0.257109 4.366133 0.0000\n#> sin12 0.01845 0.399784 0.046145 0.9632\n#> cos12 1.16269 0.380402 3.056467 0.0025\n#> sin6 -0.70555 0.358820 -1.966309 0.0506\n#> cos6 -0.97079 0.355213 -2.732980 0.0068\n#> sin4 0.78337 0.333359 2.349941 0.0197\n#> cos4 0.05479 0.333865 0.164117 0.8698\n#> ageChildren:time 1.29381 0.168883 7.661002 0.0000\n#> ageChildren:step1 -1.02662 18.354630 -0.055933 0.9554\n#> ageChildren:ramp1 7.06407 3.604192 1.959960 0.0513\n#> ageChildren:step2 -35.82645 25.442751 -1.408120 0.1606\n#> ageChildren:ramp2 -2.95530 4.260553 -0.693643 0.4887\n#> ageChildren:step3 -29.49930 19.876247 -1.484148 0.1393\n#> ageChildren:ramp3 0.62770 2.439516 0.257306 0.7972\n#> ageChildren:sin12 4.22521 3.793256 1.113874 0.2666\n#> ageChildren:cos12 36.54005 3.609352 10.123715 0.0000\n#> ageChildren:sin6 -9.76953 3.404578 -2.869528 0.0045\n#> ageChildren:cos6 -25.08251 3.370352 -7.442104 0.0000\n#> ageChildren:sin4 13.68702 3.162997 4.327230 0.0000\n#> ageChildren:cos4 14.66062 3.167797 4.628016 0.0000\n#> \n#> Correlation: \n#> (Intr) agChld time step1 ramp1 step2 ramp2 step3 \n#> ageChildren -0.105 \n#> time -0.862 0.091 \n#> step1 0.173 -0.018 -0.309 \n#> ramp1 0.057 -0.006 -0.063 -0.763 \n#> step2 -0.021 0.002 0.020 0.366 -0.711 \n#> ramp2 -0.004 0.000 0.005 0.655 -0.831 0.306 \n#> step3 -0.015 0.002 0.015 0.007 -0.031 0.300 -0.403 \n#> ramp3 -0.016 0.002 0.016 0.008 -0.023 0.514 -0.520 0.649\n#> sin12 -0.056 0.006 0.043 0.155 -0.247 0.317 0.074 0.209\n#> cos12 -0.050 0.005 0.069 -0.188 0.137 -0.028 -0.155 0.065\n#> sin6 -0.008 0.001 -0.006 0.034 0.018 -0.103 0.052 -0.126\n#> cos6 -0.010 0.001 0.002 0.047 -0.026 -0.035 0.060 -0.071\n#> sin4 -0.024 0.002 0.028 -0.033 -0.004 0.059 -0.040 0.066\n#> cos4 -0.011 0.001 0.002 0.063 -0.078 0.095 0.024 0.069\n#> ageChildren:time 0.091 -0.862 -0.105 0.033 0.007 -0.002 0.000 -0.002\n#> ageChildren:step1 -0.018 0.173 0.033 -0.105 0.080 -0.039 -0.069 -0.001\n#> ageChildren:ramp1 -0.006 0.057 0.007 0.080 -0.105 0.075 0.088 0.003\n#> ageChildren:step2 0.002 -0.021 -0.002 -0.039 0.075 -0.105 -0.032 -0.032\n#> ageChildren:ramp2 0.000 -0.004 0.000 -0.069 0.088 -0.032 -0.105 0.043\n#> ageChildren:step3 0.002 -0.015 -0.002 -0.001 0.003 -0.032 0.043 -0.105\n#> ageChildren:ramp3 0.002 -0.016 -0.002 -0.001 0.002 -0.054 0.055 -0.068\n#> ageChildren:sin12 0.006 -0.056 -0.005 -0.016 0.026 -0.033 -0.008 -0.022\n#> ageChildren:cos12 0.005 -0.050 -0.007 0.020 -0.014 0.003 0.016 -0.007\n#> ageChildren:sin6 0.001 -0.008 0.001 -0.004 -0.002 0.011 -0.005 0.013\n#> ageChildren:cos6 0.001 -0.010 0.000 -0.005 0.003 0.004 -0.006 0.007\n#> ageChildren:sin4 0.002 -0.024 -0.003 0.003 0.000 -0.006 0.004 -0.007\n#> ageChildren:cos4 0.001 -0.011 0.000 -0.007 0.008 -0.010 -0.002 -0.007\n#> ramp3 sin12 cos12 sin6 cos6 sin4 cos4 agChl:\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 0.235 \n#> cos12 0.057 -0.008 \n#> sin6 -0.099 -0.036 -0.015 \n#> cos6 -0.058 -0.011 -0.017 0.017 \n#> sin4 0.074 0.025 0.014 -0.013 -0.006 \n#> cos4 0.073 0.041 -0.007 -0.009 -0.004 0.009 \n#> ageChildren:time -0.002 -0.005 -0.007 0.001 0.000 -0.003 0.000 \n#> ageChildren:step1 -0.001 -0.016 0.020 -0.004 -0.005 0.003 -0.007 -0.309\n#> ageChildren:ramp1 0.002 0.026 -0.014 -0.002 0.003 0.000 0.008 -0.063\n#> ageChildren:step2 -0.054 -0.033 0.003 0.011 0.004 -0.006 -0.010 0.020\n#> ageChildren:ramp2 0.055 -0.008 0.016 -0.005 -0.006 0.004 -0.002 0.005\n#> ageChildren:step3 -0.068 -0.022 -0.007 0.013 0.007 -0.007 -0.007 0.015\n#> ageChildren:ramp3 -0.105 -0.025 -0.006 0.010 0.006 -0.008 -0.008 0.016\n#> ageChildren:sin12 -0.025 -0.105 0.001 0.004 0.001 -0.003 -0.004 0.043\n#> ageChildren:cos12 -0.006 0.001 -0.105 0.002 0.002 -0.002 0.001 0.069\n#> ageChildren:sin6 0.010 0.004 0.002 -0.105 -0.002 0.001 0.001 -0.006\n#> ageChildren:cos6 0.006 0.001 0.002 -0.002 -0.105 0.001 0.000 0.002\n#> ageChildren:sin4 -0.008 -0.003 -0.002 0.001 0.001 -0.105 -0.001 0.028\n#> ageChildren:cos4 -0.008 -0.004 0.001 0.001 0.000 -0.001 -0.105 0.002\n#> agChldrn:s1 agChldrn:r1 agChldrn:s2 agChldrn:r2 agChldrn:s3\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 -0.763 \n#> ageChildren:step2 0.366 -0.711 \n#> ageChildren:ramp2 0.655 -0.831 0.306 \n#> ageChildren:step3 0.007 -0.031 0.300 -0.403 \n#> ageChildren:ramp3 0.008 -0.023 0.514 -0.520 0.649 \n#> ageChildren:sin12 0.155 -0.247 0.317 0.074 0.209 \n#> ageChildren:cos12 -0.188 0.137 -0.028 -0.155 0.065 \n#> ageChildren:sin6 0.034 0.018 -0.103 0.052 -0.126 \n#> ageChildren:cos6 0.047 -0.026 -0.035 0.060 -0.071 \n#> ageChildren:sin4 -0.033 -0.004 0.059 -0.040 0.066 \n#> ageChildren:cos4 0.063 -0.078 0.095 0.024 0.069 \n#> agChldrn:r3 agChldrn:s12 agChldrn:c12 agChldrn:s6 agChldrn:c6\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 \n#> ageChildren:step2 \n#> ageChildren:ramp2 \n#> ageChildren:step3 \n#> ageChildren:ramp3 \n#> ageChildren:sin12 0.235 \n#> ageChildren:cos12 0.057 -0.008 \n#> ageChildren:sin6 -0.099 -0.036 -0.015 \n#> ageChildren:cos6 -0.058 -0.011 -0.017 0.017 \n#> ageChildren:sin4 0.074 0.025 0.014 -0.013 -0.006 \n#> ageChildren:cos4 0.073 0.041 -0.007 -0.009 -0.004 \n#> agChldrn:s4\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 \n#> ageChildren:step2 \n#> ageChildren:ramp2 \n#> ageChildren:step3 \n#> ageChildren:ramp3 \n#> ageChildren:sin12 \n#> ageChildren:cos12 \n#> ageChildren:sin6 \n#> ageChildren:cos6 \n#> ageChildren:sin4 \n#> ageChildren:cos4 0.009 \n#> \n#> Standardized residuals:\n#> Min Q1 Med Q3 Max \n#> -5.25291094 -0.42240774 0.04331099 0.45224365 3.26323939 \n#> \n#> Residual standard error: 2.608572 \n#> Degrees of freedom: 240 total; 212 residual\n\n\n\n\n\nCreated on 2026-08-29 with reprex v2.1.1 (https://reprex.tidyverse.org)", "answer_url": "https://stats.stackexchange.com/a/676996", "author": "M--", "author_url": "https://stats.stackexchange.com/users/154449/m", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-29T05:35:19+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676979, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "M--", "profile_url": "https://stats.stackexchange.com/users/154449/m", "user_type": "registered"}, "created_at": "2026-08-29T05:35:19+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "4F437721-3A00-4B4D-BA06-A2F50516CC59", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/4F437721-3A00-4B4D-BA06-A2F50516CC59/view-source"}], "score": 3, "updated_at": "2026-08-29T05:35:19+00:00"}, "other_answers": [], "product": "statistics_qa", "question": "Background\n\n\n\n\n\nData: time series of the monthly prevalence of patients receiving a given medication by age group (children or adults) from January 2016 to December 2026\n\n\n\n\nInterventions: three successive regulatory interventions implemented on September 2021, June 2022, and June 2023 to facilitate prescription of the medication (dotted lines on the figure below)\n\n\n\n\nAim: to estimate pre-intervention slope, post-intervention 1 slope, post-intervention 2 slope, and post-intervention 3 slope depending on age group to assess the impact of the interventions (i.e., interrupted time series analysis, ITS)\n\n\n\n\n\nmonth <- seq(\n from = as.Date(\"2016-01-01\"),\n to = as.Date(\"2025-12-31\"),\n by = \"month\"\n)\nmonth <- rep(month, each = 2)\nage <- rep(c(\"Children\", \"Adults\"), 120)\nprevalence <- c(\n 214.1, 11.3, 194.9, 11.1, 220.3, 11.8, 200.7, 11.7, 213.5, \n 11.7, 197.6, 11.5, 128.2, 10.3, 138.6, 9.9, 215, 12.1, 201.9, \n 11.9, 223.8, 12.2, 204.9, 12.4, 234.1, 12.7, 202, 11.9, 247, \n 13.6, 209.8, 13, 236.7, 13.4, 216.9, 13.1, 145.1, 12, 144.3, \n 11, 235.1, 13.7, 227.5, 13.9, 242.5, 13.9, 229.5, 13.8, 255.3, \n 14.5, 226.3, 13.6, 260.8, 15.2, 229.7, 14.2, 245.5, 14.7, 232.6, \n 14.4, 158.8, 13.4, 165, 12.5, 244.3, 14.8, 249.4, 15.7, 259.4, \n 15.5, 244, 15.3, 275.7, 16.2, 240.7, 14.9, 274.4, 16.2, 256.8, \n 16.3, 274.3, 16.4, 240.3, 15.5, 178.2, 15.5, 180.3, 13.6, 264, \n 16.3, 271.3, 17.4, 277.8, 16.9, 261.5, 16.8, 296.9, 17.8, 265.8, \n 17.2, 270.4, 17.9, 218.8, 16, 214.7, 16, 221.8, 16.7, 183.6, \n 16.8, 185.4, 14.9, 273.6, 18.7, 276.7, 19.3, 285, 19.4, 282.8, \n 19.9, 308.3, 20.2, 279.2, 19.3, 331.1, 22, 289.3, 21.2, 303, \n 20.9, 307.5, 21.8, 221.2, 20.9, 215.4, 18.9, 324, 23.4, 323.3, \n 24.3, 328.3, 24.7, 326.5, 25.8, 349.2, 26.1, 319.1, 25.5, 379.3, \n 29, 341.7, 28.3, 365.1, 28.6, 351.7, 30.1, 253.7, 28.6, 266.9, \n 27, 375, 33.4, 373.5, 34.4, 385.8, 35.5, 368.1, 36, 413.7, 38.8, \n 376, 37.4, 440.1, 42.5, 390.3, 40.8, 409.5, 42.6, 402, 44.9, \n 297.9, 43.4, 297.1, 40.2, 430.9, 49.3, 433.4, 51.2, 454.1, 53.1, \n 441.4, 53.4, 472.9, 57, 455.8, 57, 496.3, 62.6, 475, 63.1, 483.4, \n 63.3, 466, 65.1, 367.3, 66.2, 368.4, 58.6, 499.4, 70.8, 528.3, \n 76.1, 533.8, 76, 525.6, 77.2, 588.3, 84.8, 532.4, 81.1, 599.6, \n 90.9, 580.3, 90.8, 601.5, 93.8, 560.6, 95.1, 455.2, 96.9, 440.6, \n 83.7, 629.2, 107.7, 655.7, 112.8, 653.8, 113.2, 674.9, 118.7\n)\nd <- data.frame(month, age, prevalence)\n\npar(mfrow = c(1, 2))\nplot(prevalence ~ month, d, type = \"l\", main = \"Children\", subset = age == \"Children\")\nplot(prevalence ~ month, d, type = \"l\", main = \"Adults\", subset = age == \"Adults\")\ndev.off()\n\n\n\n\n\n[image: time series; source: https://i.sstatic.net/4a0pdydL.png] (https://i.sstatic.net/4a0pdydL.png)\n\n\n\n\nWhat I did\n\n\n\n\nI started with children.\n\n\n\n\nI constructed the ITS variables for the 3 interventions (step1, ramp1, step2, ramp2, step3, ramp3) following Schaffer et al. (https://doi.org/10.1186/s12874-021-01235-8) and Xiao et al. (https://doi.org/10.1093/ije/dyaa148)\n\n\n\n\nd2 <- subset(d, age == \"Children\")\nd2$prevalence <- ts(d2$prevalence, start = 2016, frequency = 12)\n\ninterventions <- as.Date(c(\"2021-09-13\", \"2022-06-02\", \"2023-06-29\"))\n\nstep1 <- as.numeric(d2$month >= interventions[1])\nramp1 <- ifelse(step1 == 1, cumsum(step1) - 1, 0)\n\nstep2 <- as.numeric(d2$month >= interventions[2])\nramp2 <- ifelse(step2 == 1, cumsum(step2) - 1, 0)\n\nstep3 <- as.numeric(d2$month >= interventions[3])\nramp3 <- ifelse(step3 == 1, cumsum(step3) - 1, 0)\n\ntime <- seq(from = 0, along.with = d2$month)\n\nxreg <- cbind(time, step1, ramp1, step2, ramp2, step3, ramp3)\n\ncbind(d2, xreg)\n# month age prevalence time step1 ramp1 step2 ramp2 step3 ramp3\n# 1 2016-01-01 Children 214.1 0 0 0 0 0 0 0\n# 3 2016-02-01 Children 194.9 1 0 0 0 0 0 0\n# 5 2016-03-01 Children 220.3 2 0 0 0 0 0 0\n# 7 2016-04-01 Children 200.7 3 0 0 0 0 0 0\n# 9 2016-05-01 Children 213.5 4 0 0 0 0 0 0\n# 11 2016-06-01 Children 197.6 5 0 0 0 0 0 0\n# 13 2016-07-01 Children 128.2 6 0 0 0 0 0 0\n# 15 2016-08-01 Children 138.6 7 0 0 0 0 0 0\n# 17 2016-09-01 Children 215.0 8 0 0 0 0 0 0\n# 19 2016-10-01 Children 201.9 9 0 0 0 0 0 0\n# 21 2016-11-01 Children 223.8 10 0 0 0 0 0 0\n# 23 2016-12-01 Children 204.9 11 0 0 0 0 0 0\n# 25 2017-01-01 Children 234.1 12 0 0 0 0 0 0\n# 27 2017-02-01 Children 202.0 13 0 0 0 0 0 0\n# 29 2017-03-01 Children 247.0 14 0 0 0 0 0 0\n# 31 2017-04-01 Children 209.8 15 0 0 0 0 0 0\n# 33 2017-05-01 Children 236.7 16 0 0 0 0 0 0\n# 35 2017-06-01 Children 216.9 17 0 0 0 0 0 0\n# 37 2017-07-01 Children 145.1 18 0 0 0 0 0 0\n# 39 2017-08-01 Children 144.3 19 0 0 0 0 0 0\n# 41 2017-09-01 Children 235.1 20 0 0 0 0 0 0\n# 43 2017-10-01 Children 227.5 21 0 0 0 0 0 0\n# 45 2017-11-01 Children 242.5 22 0 0 0 0 0 0\n# 47 2017-12-01 Children 229.5 23 0 0 0 0 0 0\n# 49 2018-01-01 Children 255.3 24 0 0 0 0 0 0\n# 51 2018-02-01 Children 226.3 25 0 0 0 0 0 0\n# 53 2018-03-01 Children 260.8 26 0 0 0 0 0 0\n# 55 2018-04-01 Children 229.7 27 0 0 0 0 0 0\n# 57 2018-05-01 Children 245.5 28 0 0 0 0 0 0\n# 59 2018-06-01 Children 232.6 29 0 0 0 0 0 0\n# 61 2018-07-01 Children 158.8 30 0 0 0 0 0 0\n# 63 2018-08-01 Children 165.0 31 0 0 0 0 0 0\n# 65 2018-09-01 Children 244.3 32 0 0 0 0 0 0\n# 67 2018-10-01 Children 249.4 33 0 0 0 0 0 0\n# 69 2018-11-01 Children 259.4 34 0 0 0 0 0 0\n# 71 2018-12-01 Children 244.0 35 0 0 0 0 0 0\n# 73 2019-01-01 Children 275.7 36 0 0 0 0 0 0\n# 75 2019-02-01 Children 240.7 37 0 0 0 0 0 0\n# 77 2019-03-01 Children 274.4 38 0 0 0 0 0 0\n# 79 2019-04-01 Children 256.8 39 0 0 0 0 0 0\n# 81 2019-05-01 Children 274.3 40 0 0 0 0 0 0\n# 83 2019-06-01 Children 240.3 41 0 0 0 0 0 0\n# 85 2019-07-01 Children 178.2 42 0 0 0 0 0 0\n# 87 2019-08-01 Children 180.3 43 0 0 0 0 0 0\n# 89 2019-09-01 Children 264.0 44 0 0 0 0 0 0\n# 91 2019-10-01 Children 271.3 45 0 0 0 0 0 0\n# 93 2019-11-01 Children 277.8 46 0 0 0 0 0 0\n# 95 2019-12-01 Children 261.5 47 0 0 0 0 0 0\n# 97 2020-01-01 Children 296.9 48 0 0 0 0 0 0\n# 99 2020-02-01 Children 265.8 49 0 0 0 0 0 0\n# 101 2020-03-01 Children 270.4 50 0 0 0 0 0 0\n# 103 2020-04-01 Children 218.8 51 0 0 0 0 0 0\n# 105 2020-05-01 Children 214.7 52 0 0 0 0 0 0\n# 107 2020-06-01 Children 221.8 53 0 0 0 0 0 0\n# 109 2020-07-01 Children 183.6 54 0 0 0 0 0 0\n# 111 2020-08-01 Children 185.4 55 0 0 0 0 0 0\n# 113 2020-09-01 Children 273.6 56 0 0 0 0 0 0\n# 115 2020-10-01 Children 276.7 57 0 0 0 0 0 0\n# 117 2020-11-01 Children 285.0 58 0 0 0 0 0 0\n# 119 2020-12-01 Children 282.8 59 0 0 0 0 0 0\n# 121 2021-01-01 Children 308.3 60 0 0 0 0 0 0\n# 123 2021-02-01 Children 279.2 61 0 0 0 0 0 0\n# 125 2021-03-01 Children 331.1 62 0 0 0 0 0 0\n# 127 2021-04-01 Children 289.3 63 0 0 0 0 0 0\n# 129 2021-05-01 Children 303.0 64 0 0 0 0 0 0\n# 131 2021-06-01 Children 307.5 65 0 0 0 0 0 0\n# 133 2021-07-01 Children 221.2 66 0 0 0 0 0 0\n# 135 2021-08-01 Children 215.4 67 0 0 0 0 0 0\n# 137 2021-09-01 Children 324.0 68 0 0 0 0 0 0\n# 139 2021-10-01 Children 323.3 69 1 0 0 0 0 0\n# 141 2021-11-01 Children 328.3 70 1 1 0 0 0 0\n# 143 2021-12-01 Children 326.5 71 1 2 0 0 0 0\n# 145 2022-01-01 Children 349.2 72 1 3 0 0 0 0\n# 147 2022-02-01 Children 319.1 73 1 4 0 0 0 0\n# 149 2022-03-01 Children 379.3 74 1 5 0 0 0 0\n# 151 2022-04-01 Children 341.7 75 1 6 0 0 0 0\n# 153 2022-05-01 Children 365.1 76 1 7 0 0 0 0\n# 155 2022-06-01 Children 351.7 77 1 8 0 0 0 0\n# 157 2022-07-01 Children 253.7 78 1 9 1 0 0 0\n# 159 2022-08-01 Children 266.9 79 1 10 1 1 0 0\n# 161 2022-09-01 Children 375.0 80 1 11 1 2 0 0\n# 163 2022-10-01 Children 373.5 81 1 12 1 3 0 0\n# 165 2022-11-01 Children 385.8 82 1 13 1 4 0 0\n# 167 2022-12-01 Children 368.1 83 1 14 1 5 0 0\n# 169 2023-01-01 Children 413.7 84 1 15 1 6 0 0\n# 171 2023-02-01 Children 376.0 85 1 16 1 7 0 0\n# 173 2023-03-01 Children 440.1 86 1 17 1 8 0 0\n# 175 2023-04-01 Children 390.3 87 1 18 1 9 0 0\n# 177 2023-05-01 Children 409.5 88 1 19 1 10 0 0\n# 179 2023-06-01 Children 402.0 89 1 20 1 11 0 0\n# 181 2023-07-01 Children 297.9 90 1 21 1 12 1 0\n# 183 2023-08-01 Children 297.1 91 1 22 1 13 1 1\n# 185 2023-09-01 Children 430.9 92 1 23 1 14 1 2\n# 187 2023-10-01 Children 433.4 93 1 24 1 15 1 3\n# 189 2023-11-01 Children 454.1 94 1 25 1 16 1 4\n# 191 2023-12-01 Children 441.4 95 1 26 1 17 1 5\n# 193 2024-01-01 Children 472.9 96 1 27 1 18 1 6\n# 195 2024-02-01 Children 455.8 97 1 28 1 19 1 7\n# 197 2024-03-01 Children 496.3 98 1 29 1 20 1 8\n# 199 2024-04-01 Children 475.0 99 1 30 1 21 1 9\n# 201 2024-05-01 Children 483.4 100 1 31 1 22 1 10\n# 203 2024-06-01 Children 466.0 101 1 32 1 23 1 11\n# 205 2024-07-01 Children 367.3 102 1 33 1 24 1 12\n# 207 2024-08-01 Children 368.4 103 1 34 1 25 1 13\n# 209 2024-09-01 Children 499.4 104 1 35 1 26 1 14\n# 211 2024-10-01 Children 528.3 105 1 36 1 27 1 15\n# 213 2024-11-01 Children 533.8 106 1 37 1 28 1 16\n# 215 2024-12-01 Children 525.6 107 1 38 1 29 1 17\n# 217 2025-01-01 Children 588.3 108 1 39 1 30 1 18\n# 219 2025-02-01 Children 532.4 109 1 40 1 31 1 19\n# 221 2025-03-01 Children 599.6 110 1 41 1 32 1 20\n# 223 2025-04-01 Children 580.3 111 1 42 1 33 1 21\n# 225 2025-05-01 Children 601.5 112 1 43 1 34 1 22\n# 227 2025-06-01 Children 560.6 113 1 44 1 35 1 23\n# 229 2025-07-01 Children 455.2 114 1 45 1 36 1 24\n# 231 2025-08-01 Children 440.6 115 1 46 1 37 1 25\n# 233 2025-09-01 Children 629.2 116 1 47 1 38 1 26\n# 235 2025-10-01 Children 655.7 117 1 48 1 39 1 27\n# 237 2025-11-01 Children 653.8 118 1 49 1 40 1 28\n# 239 2025-12-01 Children 674.9 119 1 50 1 41 1 29\n\n\n\n\n\nAccording to this thread (https://stats.stackexchange.com/q/665216/), I included the ITS variables for the 3 interventions in a single model. I used forecast::auto.arima() to select (p, q) and (P, Q) parameters automatically.\n\n\n\n\nAccording to Schaffer et al. (https://doi.org/10.1186/s12874-021-01235-8), I included a time variable and set $d = 0$ and $D = 0$ (but I am not sure that I need both $d = 0$ and $D = 0$): (emphasis mine)\n\n\n\n\n\n\n\nIn ITS analysis, ARIMA forecasts $Y_t$ in the absence of the intervention (the “counterfactual”) and determines how the observed diverges from this forecast. Unlike segmented regression, including time or seasonal dummy variables in the ARIMA model is not necessary, as ARIMA can eliminate trends and seasonality through differencing. If the trend is eliminated via differencing then the pre- and post-intervention trends cannot be estimated from the model. However, if estimation of the pre- and/or post-intervention slope is desired, this can be accommodated by including time as a covariate and incorporating AR and MA terms to address autocorrelation (e.g. ARMA models) [21 (https://doi.org/10.1186/s12889-017-4998-9), 22 (https://doi.org/10.3111/13696998.2011.626097)].\n\n\n\n\n\n\n\nfit <- forecast::auto.arima(\n d2$prevalence,\n d = 0,\n D = 0,\n stepwise = FALSE,\n trace = TRUE,\n approximation = FALSE,\n xreg = xreg\n)\n# \n# Regression with ARIMA(0,0,0) errors : 1422.701\n# Regression with ARIMA(0,0,0) errors : 1256.821\n# Regression with ARIMA(0,0,0)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,0)(0,0,1)[12] errors : 1186.586\n# Regression with ARIMA(0,0,0)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,0)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,2)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,1) errors : 1348.712\n# Regression with ARIMA(0,0,1) errors : 1237.018\n# Regression with ARIMA(0,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,1)(0,0,1)[12] errors : 1167.011\n# Regression with ARIMA(0,0,1)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,1)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,1)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,1)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,2)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,1)(2,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,2) errors : 1304.633\n# Regression with ARIMA(0,0,2) errors : 1235.236\n# Regression with ARIMA(0,0,2)(0,0,1)[12] errors : 1226.35\n# Regression with ARIMA(0,0,2)(0,0,1)[12] errors : 1164.06\n# Regression with ARIMA(0,0,2)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,2)(0,0,2)[12] errors : 1122.153\n# ARIMA(0,0,2)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,2)(1,0,0)[12] errors : Inf\n# ARIMA(0,0,2)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,2)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,2)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,2)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,2)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,3) errors : 1301.979\n# Regression with ARIMA(0,0,3) errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,1)[12] errors : 1166.444\n# Regression with ARIMA(0,0,3)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,3)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,3)(1,0,0)[12] errors : Inf\n# ARIMA(0,0,3)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,3)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,3)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,3)(2,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,4) errors : Inf\n# Regression with ARIMA(0,0,4) errors : Inf\n# Regression with ARIMA(0,0,4)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,4)(0,0,1)[12] errors : 1167.795\n# ARIMA(0,0,4)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,4)(1,0,0)[12] errors : Inf\n# Regression with ARIMA(0,0,5) errors : Inf\n# Regression with ARIMA(0,0,5) errors : Inf\n# Regression with ARIMA(1,0,0) errors : 1269.039\n# Regression with ARIMA(1,0,0) errors : 1236.551\n# Regression with ARIMA(1,0,0)(0,0,1)[12] errors : 1191.463\n# Regression with ARIMA(1,0,0)(0,0,1)[12] errors : 1163.925\n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors : 1144.206\n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors : 1122.139\n# ARIMA(1,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,0)(2,0,0)[12] errors : Inf\n# ARIMA(1,0,0)(2,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,0)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(2,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,0)(2,0,2)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,1) errors : 1269.741\n# Regression with ARIMA(1,0,1) errors : 1237.127\n# Regression with ARIMA(1,0,1)(0,0,1)[12] errors : 1191.287\n# Regression with ARIMA(1,0,1)(0,0,1)[12] errors : 1165.649\n# Regression with ARIMA(1,0,1)(0,0,2)[12] errors : 1145.611\n# Regression with ARIMA(1,0,1)(0,0,2)[12] errors : 1123.56\n# ARIMA(1,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(1,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,1)(2,0,0)[12] errors : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,2) errors : Inf\n# ARIMA(1,0,2) with non-zero mean : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,2) errors : Inf\n# ARIMA(1,0,2) with non-zero mean : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,2)[12] errors : Inf\n# ARIMA(1,0,2)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,2)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,2)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,2)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,2)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,2)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(1,0,3) errors : Inf\n# Regression with ARIMA(1,0,3) errors : Inf\n# Regression with ARIMA(1,0,3)(0,0,1)[12] errors : 1196.785\n# Regression with ARIMA(1,0,3)(0,0,1)[12] errors : Inf\n# ARIMA(1,0,3)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,3)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,4) errors : Inf\n# Regression with ARIMA(1,0,4) errors : Inf\n# Regression with ARIMA(2,0,0) errors : 1270.227\n# Regression with ARIMA(2,0,0) errors : 1235.453\n# Regression with ARIMA(2,0,0)(0,0,1)[12] errors : 1191.855\n# Regression with ARIMA(2,0,0)(0,0,1)[12] errors : 1165.096\n# Regression with ARIMA(2,0,0)(0,0,2)[12] errors : 1145.887\n# Regression with ARIMA(2,0,0)(0,0,2)[12] errors : 1123.031\n# ARIMA(2,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,0)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(2,0,1)[12] with zero mean : Inf\n# Regression with ARIMA(2,0,0)(2,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1) errors : Inf\n# Regression with ARIMA(2,0,1) errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,2)[12] errors : Inf\n# ARIMA(2,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,1)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(2,0,1)(1,0,1)[12] errors : Inf\n# ARIMA(2,0,1)(1,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(2,0,1)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(2,0,2) errors : Inf\n# Regression with ARIMA(2,0,2) errors : Inf\n# ARIMA(2,0,2)(0,0,1)[12] with zero mean : Inf\n# Regression with ARIMA(2,0,2)(0,0,1)[12] errors : Inf\n# ARIMA(2,0,2)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,2)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,3) with zero mean : Inf\n# Regression with ARIMA(2,0,3) errors : Inf\n# Regression with ARIMA(3,0,0) errors : 1271.552\n# Regression with ARIMA(3,0,0) errors : 1234.442\n# Regression with ARIMA(3,0,0)(0,0,1)[12] errors : 1193.944\n# Regression with ARIMA(3,0,0)(0,0,1)[12] errors : 1165.004\n# Regression with ARIMA(3,0,0)(0,0,2)[12] errors : 1147.337\n# Regression with ARIMA(3,0,0)(0,0,2)[12] errors : 1124.69\n# ARIMA(3,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(3,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(3,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(3,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,0)(2,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(3,0,1) errors : Inf\n# Regression with ARIMA(3,0,1) errors : Inf\n# Regression with ARIMA(3,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(3,0,1)(0,0,1)[12] errors : Inf\n# ARIMA(3,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,1)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(3,0,2) errors : Inf\n# Regression with ARIMA(3,0,2) errors : Inf\n# Regression with ARIMA(4,0,0) errors : 1268.176\n# Regression with ARIMA(4,0,0) errors : 1236.501\n# Regression with ARIMA(4,0,0)(0,0,1)[12] errors : 1191.278\n# Regression with ARIMA(4,0,0)(0,0,1)[12] errors : 1167.39\n# ARIMA(4,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(4,0,0)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(4,0,1) errors : Inf\n# Regression with ARIMA(4,0,1) errors : Inf\n# Regression with ARIMA(5,0,0) errors : 1270.388\n# Regression with ARIMA(5,0,0) errors : 1226.449\n# \n# \n# \n# Best model: Regression with ARIMA(1,0,0)(0,0,2)[12] errors \n# \n\nsummary(fit)\n# Series: d2$prevalence \n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors \n# \n# Coefficients:\n# ar1 sma1 sma2 intercept time step1 ramp1 step2 ramp2 step3 ramp3\n# 0.5256 1.0413 0.6987 188.8179 1.5717 3.3284 2.1966 -5.6181 0.4640 0.4378 4.6447\n# s.e. 0.0800 0.1536 0.1312 20.6895 0.4724 13.0492 3.5219 23.9955 3.8197 19.1790 2.5724\n# \n# sigma^2 = 494.9: log likelihood = -547.61\n# AIC=1119.22 AICc=1122.14 BIC=1152.67\n# \n# Training set error measures:\n# ME RMSE MAE MPE MAPE MASE ACF1\n# Training set -0.2760281 21.20232 16.21419 -0.9807653 5.578825 0.3604042 0.05861382\n\n\n\n\n\nQuestion\n\n\n\n\nShould I just apply the same strategy to adults? Or can such models handle correlated/stratified time series?\n\n\n\n\nI guess that there is a better strategy than fitting separate models within each subgroup. However, I suspect that I cannot simply stack the time series and include age in xreg. Additionally, I probably need an interaction terms (e.g. time * age).\n\n\n\n\nI have also read about nlme::gls() (https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/gls.html) but I would like guidance/confirmation whether it is more appropriate.\n\n\n\n\n\n\n\nBTW, I am not satisfied with the fit, but I will explore that once I have decided the modelling strategy.\n\n\n\n\nres <- residuals(fit)\nl <- 36\n\npar(mfrow = c(3, 1))\nplot(res)\nacf(res, lag.max = l)\npacf(res, lag.max = l)\ndev.off()\n\nBox.test(res, lag = 24, type = \"Ljung-Box\", fitdf = 3)\n# \n# Box-Ljung test\n# \n# data: res\n# X-squared = 49.814, df = 21, p-value = 0.0003872\n# \n\n\n\n\n\n[image: residuals; source: https://i.sstatic.net/pzs11NTf.png] (https://i.sstatic.net/pzs11NTf.png)", "record_id": "Scientific-Statistics-QA:stats:676979", "selected_answer": {"answer_html": "<p>Rather than fitting separate models, you can handle both age groups in a single <code>gls()</code> model with <code>age</code> interactions. This has two advantages over separate <code>auto.arima()</code> models: you get formal tests of whether intervention effects differ between groups via the interaction terms, and you can account for the fact that the two series have very different variances.</p>\n<p><code>age * (time + step1 + ramp1 + ...)</code> gives each group its own intercept, pre-intervention slope, and intervention effects, plus tests of whether they differ. Then, <code>corAR1(form = ~ 1 | age)</code> fits AR(1) autocorrelation within each group and <code>varIdent(form = ~ 1 | age)</code> allows different residual variances per group, which matters here since children's prevalence is an order of magnitude larger.</p>\n<pre class=\"lang-r prettyprint-override\"><code>library(nlme)\nlibrary(dplyr)\n\ninterventions &lt;- as.Date(c(&quot;2021-09-13&quot;, &quot;2022-06-02&quot;, &quot;2023-06-29&quot;))\n\nd_its &lt;- d |&gt;\n arrange(age, month) |&gt;\n mutate(.by = age,\n time = row_number() - 1,\n step1 = as.numeric(month &gt;= interventions[1]),\n ramp1 = ifelse(step1 == 1, cumsum(step1) - 1, 0),\n step2 = as.numeric(month &gt;= interventions[2]),\n ramp2 = ifelse(step2 == 1, cumsum(step2) - 1, 0),\n step3 = as.numeric(month &gt;= interventions[3]),\n ramp3 = ifelse(step3 == 1, cumsum(step3) - 1, 0),\n sin12 = sin(2 * pi * time / 12),\n cos12 = cos(2 * pi * time / 12),\n sin6 = sin(2 * pi * time / 6),\n cos6 = cos(2 * pi * time / 6),\n sin4 = sin(2 * pi * time / 4),\n cos4 = cos(2 * pi * time / 4)\n ) |&gt;\n mutate(age = factor(age))\n\nfit &lt;- gls(\n prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 +\n sin12 + cos12 + sin6 + cos6 + sin4 + cos4),\n data = d_its,\n correlation = corAR1(form = ~ 1 | age),\n weights = varIdent(form = ~ 1 | age)\n)\n</code></pre>\n<p>The Fourier terms (<code>sin12</code>, <code>cos12</code>, etc.) handle the recurring seasonal pattern, which is more stable than trying to absorb it through ARMA error terms (see <a href=\"https://otexts.com/fpp3/dhr.html\" rel=\"nofollow noreferrer\">https://otexts.com/fpp3/dhr.html</a>).</p>\n<p>The reference level is Adults, so coefficients are interpreted as:</p>\n<ul>\n<li><code>time</code>: pre-intervention slope for adults (prevalence per month)</li>\n<li><code>ageChildren:time</code>: <em>additional</em> slope for children;</li>\n</ul>\n<p>The same logic applies for all <code>ramp</code> and <code>step</code> terms.</p>\n<pre class=\"lang-r prettyprint-override\"><code>coef(fit) |&gt;\n (\\(e) data.frame(\n Segment = c(&quot;Pre&quot;, &quot;Post-1&quot;, &quot;Post-2&quot;, &quot;Post-3&quot;),\n Adults = round(cumsum(c(e[&quot;time&quot;],\n e[&quot;ramp1&quot;],\n e[&quot;ramp2&quot;],\n e[&quot;ramp3&quot;])), 3),\n Children = round(cumsum(c(e[&quot;time&quot;] + e[&quot;ageChildren:time&quot;],\n e[&quot;ramp1&quot;] + e[&quot;ageChildren:ramp1&quot;],\n e[&quot;ramp2&quot;] + e[&quot;ageChildren:ramp2&quot;],\n e[&quot;ramp3&quot;] + e[&quot;ageChildren:ramp3&quot;])), 3)\n))()\n\n#&gt; Segment Adults Children\n#&gt; time Pre 0.152 1.445\n#&gt; ramp1 Post-1 0.919 9.276\n#&gt; ramp2 Post-2 1.265 6.667\n#&gt; ramp3 Post-3 2.387 8.418\n</code></pre>\n<pre class=\"lang-r prettyprint-override\"><code>par(mfrow = c(1, 2))\nacf(residuals(fit)[d_its<span class=\"math-container\">$age == \"Adults\"], main = \"Adults\")\nacf(residuals(fit)[d_its$</span>age == &quot;Children&quot;], main = &quot;Children&quot;)\ndev.off()\n</code></pre>\n<p><img src=\"https://i.sstatic.net/2fnDCjiM.png\" alt=\"\" /></p>\n<pre class=\"lang-r prettyprint-override\"><code>summary(fit)\n\n#&gt; Generalized least squares fit by REML\n#&gt; Model: prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 + sin12 + cos12 + sin6 + cos6 + sin4 + cos4) \n#&gt; Data: d_its \n#&gt; AIC BIC logLik\n#&gt; 1665.172 1769.226 -801.5858\n#&gt; \n#&gt; Correlation Structure: AR(1)\n#&gt; Formula: ~1 | age \n#&gt; Parameter estimate(s):\n#&gt; Phi \n#&gt; 0.1231768 \n#&gt; Variance function:\n#&gt; Structure: Different standard deviations per stratum\n#&gt; Formula: ~1 | age \n#&gt; Parameter estimates:\n#&gt; Adults Children \n#&gt; 1.000000 9.435418 \n#&gt; \n#&gt; Coefficients:\n#&gt; Value Std.Error t-value p-value\n#&gt; (Intercept) 10.25450 0.701853 14.610597 0.0000\n#&gt; ageChildren 177.16318 6.659369 26.603599 0.0000\n#&gt; time 0.15162 0.017799 8.518225 0.0000\n#&gt; step1 1.83019 1.934456 0.946100 0.3452\n#&gt; ramp1 0.76689 0.379858 2.018892 0.0448\n#&gt; step2 -0.98456 2.681497 -0.367167 0.7139\n#&gt; ramp2 0.34620 0.449034 0.770992 0.4416\n#&gt; step3 -4.38832 2.094825 -2.094838 0.0374\n#&gt; ramp3 1.12257 0.257109 4.366133 0.0000\n#&gt; sin12 0.01845 0.399784 0.046145 0.9632\n#&gt; cos12 1.16269 0.380402 3.056467 0.0025\n#&gt; sin6 -0.70555 0.358820 -1.966309 0.0506\n#&gt; cos6 -0.97079 0.355213 -2.732980 0.0068\n#&gt; sin4 0.78337 0.333359 2.349941 0.0197\n#&gt; cos4 0.05479 0.333865 0.164117 0.8698\n#&gt; ageChildren:time 1.29381 0.168883 7.661002 0.0000\n#&gt; ageChildren:step1 -1.02662 18.354630 -0.055933 0.9554\n#&gt; ageChildren:ramp1 7.06407 3.604192 1.959960 0.0513\n#&gt; ageChildren:step2 -35.82645 25.442751 -1.408120 0.1606\n#&gt; ageChildren:ramp2 -2.95530 4.260553 -0.693643 0.4887\n#&gt; ageChildren:step3 -29.49930 19.876247 -1.484148 0.1393\n#&gt; ageChildren:ramp3 0.62770 2.439516 0.257306 0.7972\n#&gt; ageChildren:sin12 4.22521 3.793256 1.113874 0.2666\n#&gt; ageChildren:cos12 36.54005 3.609352 10.123715 0.0000\n#&gt; ageChildren:sin6 -9.76953 3.404578 -2.869528 0.0045\n#&gt; ageChildren:cos6 -25.08251 3.370352 -7.442104 0.0000\n#&gt; ageChildren:sin4 13.68702 3.162997 4.327230 0.0000\n#&gt; ageChildren:cos4 14.66062 3.167797 4.628016 0.0000\n#&gt; \n#&gt; Correlation: \n#&gt; (Intr) agChld time step1 ramp1 step2 ramp2 step3 \n#&gt; ageChildren -0.105 \n#&gt; time -0.862 0.091 \n#&gt; step1 0.173 -0.018 -0.309 \n#&gt; ramp1 0.057 -0.006 -0.063 -0.763 \n#&gt; step2 -0.021 0.002 0.020 0.366 -0.711 \n#&gt; ramp2 -0.004 0.000 0.005 0.655 -0.831 0.306 \n#&gt; step3 -0.015 0.002 0.015 0.007 -0.031 0.300 -0.403 \n#&gt; ramp3 -0.016 0.002 0.016 0.008 -0.023 0.514 -0.520 0.649\n#&gt; sin12 -0.056 0.006 0.043 0.155 -0.247 0.317 0.074 0.209\n#&gt; cos12 -0.050 0.005 0.069 -0.188 0.137 -0.028 -0.155 0.065\n#&gt; sin6 -0.008 0.001 -0.006 0.034 0.018 -0.103 0.052 -0.126\n#&gt; cos6 -0.010 0.001 0.002 0.047 -0.026 -0.035 0.060 -0.071\n#&gt; sin4 -0.024 0.002 0.028 -0.033 -0.004 0.059 -0.040 0.066\n#&gt; cos4 -0.011 0.001 0.002 0.063 -0.078 0.095 0.024 0.069\n#&gt; ageChildren:time 0.091 -0.862 -0.105 0.033 0.007 -0.002 0.000 -0.002\n#&gt; ageChildren:step1 -0.018 0.173 0.033 -0.105 0.080 -0.039 -0.069 -0.001\n#&gt; ageChildren:ramp1 -0.006 0.057 0.007 0.080 -0.105 0.075 0.088 0.003\n#&gt; ageChildren:step2 0.002 -0.021 -0.002 -0.039 0.075 -0.105 -0.032 -0.032\n#&gt; ageChildren:ramp2 0.000 -0.004 0.000 -0.069 0.088 -0.032 -0.105 0.043\n#&gt; ageChildren:step3 0.002 -0.015 -0.002 -0.001 0.003 -0.032 0.043 -0.105\n#&gt; ageChildren:ramp3 0.002 -0.016 -0.002 -0.001 0.002 -0.054 0.055 -0.068\n#&gt; ageChildren:sin12 0.006 -0.056 -0.005 -0.016 0.026 -0.033 -0.008 -0.022\n#&gt; ageChildren:cos12 0.005 -0.050 -0.007 0.020 -0.014 0.003 0.016 -0.007\n#&gt; ageChildren:sin6 0.001 -0.008 0.001 -0.004 -0.002 0.011 -0.005 0.013\n#&gt; ageChildren:cos6 0.001 -0.010 0.000 -0.005 0.003 0.004 -0.006 0.007\n#&gt; ageChildren:sin4 0.002 -0.024 -0.003 0.003 0.000 -0.006 0.004 -0.007\n#&gt; ageChildren:cos4 0.001 -0.011 0.000 -0.007 0.008 -0.010 -0.002 -0.007\n#&gt; ramp3 sin12 cos12 sin6 cos6 sin4 cos4 agChl:\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 0.235 \n#&gt; cos12 0.057 -0.008 \n#&gt; sin6 -0.099 -0.036 -0.015 \n#&gt; cos6 -0.058 -0.011 -0.017 0.017 \n#&gt; sin4 0.074 0.025 0.014 -0.013 -0.006 \n#&gt; cos4 0.073 0.041 -0.007 -0.009 -0.004 0.009 \n#&gt; ageChildren:time -0.002 -0.005 -0.007 0.001 0.000 -0.003 0.000 \n#&gt; ageChildren:step1 -0.001 -0.016 0.020 -0.004 -0.005 0.003 -0.007 -0.309\n#&gt; ageChildren:ramp1 0.002 0.026 -0.014 -0.002 0.003 0.000 0.008 -0.063\n#&gt; ageChildren:step2 -0.054 -0.033 0.003 0.011 0.004 -0.006 -0.010 0.020\n#&gt; ageChildren:ramp2 0.055 -0.008 0.016 -0.005 -0.006 0.004 -0.002 0.005\n#&gt; ageChildren:step3 -0.068 -0.022 -0.007 0.013 0.007 -0.007 -0.007 0.015\n#&gt; ageChildren:ramp3 -0.105 -0.025 -0.006 0.010 0.006 -0.008 -0.008 0.016\n#&gt; ageChildren:sin12 -0.025 -0.105 0.001 0.004 0.001 -0.003 -0.004 0.043\n#&gt; ageChildren:cos12 -0.006 0.001 -0.105 0.002 0.002 -0.002 0.001 0.069\n#&gt; ageChildren:sin6 0.010 0.004 0.002 -0.105 -0.002 0.001 0.001 -0.006\n#&gt; ageChildren:cos6 0.006 0.001 0.002 -0.002 -0.105 0.001 0.000 0.002\n#&gt; ageChildren:sin4 -0.008 -0.003 -0.002 0.001 0.001 -0.105 -0.001 0.028\n#&gt; ageChildren:cos4 -0.008 -0.004 0.001 0.001 0.000 -0.001 -0.105 0.002\n#&gt; agChldrn:s1 agChldrn:r1 agChldrn:s2 agChldrn:r2 agChldrn:s3\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 -0.763 \n#&gt; ageChildren:step2 0.366 -0.711 \n#&gt; ageChildren:ramp2 0.655 -0.831 0.306 \n#&gt; ageChildren:step3 0.007 -0.031 0.300 -0.403 \n#&gt; ageChildren:ramp3 0.008 -0.023 0.514 -0.520 0.649 \n#&gt; ageChildren:sin12 0.155 -0.247 0.317 0.074 0.209 \n#&gt; ageChildren:cos12 -0.188 0.137 -0.028 -0.155 0.065 \n#&gt; ageChildren:sin6 0.034 0.018 -0.103 0.052 -0.126 \n#&gt; ageChildren:cos6 0.047 -0.026 -0.035 0.060 -0.071 \n#&gt; ageChildren:sin4 -0.033 -0.004 0.059 -0.040 0.066 \n#&gt; ageChildren:cos4 0.063 -0.078 0.095 0.024 0.069 \n#&gt; agChldrn:r3 agChldrn:s12 agChldrn:c12 agChldrn:s6 agChldrn:c6\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 \n#&gt; ageChildren:step2 \n#&gt; ageChildren:ramp2 \n#&gt; ageChildren:step3 \n#&gt; ageChildren:ramp3 \n#&gt; ageChildren:sin12 0.235 \n#&gt; ageChildren:cos12 0.057 -0.008 \n#&gt; ageChildren:sin6 -0.099 -0.036 -0.015 \n#&gt; ageChildren:cos6 -0.058 -0.011 -0.017 0.017 \n#&gt; ageChildren:sin4 0.074 0.025 0.014 -0.013 -0.006 \n#&gt; ageChildren:cos4 0.073 0.041 -0.007 -0.009 -0.004 \n#&gt; agChldrn:s4\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 \n#&gt; ageChildren:step2 \n#&gt; ageChildren:ramp2 \n#&gt; ageChildren:step3 \n#&gt; ageChildren:ramp3 \n#&gt; ageChildren:sin12 \n#&gt; ageChildren:cos12 \n#&gt; ageChildren:sin6 \n#&gt; ageChildren:cos6 \n#&gt; ageChildren:sin4 \n#&gt; ageChildren:cos4 0.009 \n#&gt; \n#&gt; Standardized residuals:\n#&gt; Min Q1 Med Q3 Max \n#&gt; -5.25291094 -0.42240774 0.04331099 0.45224365 3.26323939 \n#&gt; \n#&gt; Residual standard error: 2.608572 \n#&gt; Degrees of freedom: 240 total; 212 residual\n</code></pre>\n<p><sup>Created on 2026-08-29 with <a href=\"https://reprex.tidyverse.org\" rel=\"nofollow noreferrer\">reprex v2.1.1</a></sup></p>\n", "answer_id": 676996, "answer_text": "Rather than fitting separate models, you can handle both age groups in a single gls() model with age interactions. This has two advantages over separate auto.arima() models: you get formal tests of whether intervention effects differ between groups via the interaction terms, and you can account for the fact that the two series have very different variances.\n\n\n\n\nage * (time + step1 + ramp1 + ...) gives each group its own intercept, pre-intervention slope, and intervention effects, plus tests of whether they differ. Then, corAR1(form = ~ 1 | age) fits AR(1) autocorrelation within each group and varIdent(form = ~ 1 | age) allows different residual variances per group, which matters here since children's prevalence is an order of magnitude larger.\n\n\n\n\nlibrary(nlme)\nlibrary(dplyr)\n\ninterventions <- as.Date(c(\"2021-09-13\", \"2022-06-02\", \"2023-06-29\"))\n\nd_its <- d |>\n arrange(age, month) |>\n mutate(.by = age,\n time = row_number() - 1,\n step1 = as.numeric(month >= interventions[1]),\n ramp1 = ifelse(step1 == 1, cumsum(step1) - 1, 0),\n step2 = as.numeric(month >= interventions[2]),\n ramp2 = ifelse(step2 == 1, cumsum(step2) - 1, 0),\n step3 = as.numeric(month >= interventions[3]),\n ramp3 = ifelse(step3 == 1, cumsum(step3) - 1, 0),\n sin12 = sin(2 * pi * time / 12),\n cos12 = cos(2 * pi * time / 12),\n sin6 = sin(2 * pi * time / 6),\n cos6 = cos(2 * pi * time / 6),\n sin4 = sin(2 * pi * time / 4),\n cos4 = cos(2 * pi * time / 4)\n ) |>\n mutate(age = factor(age))\n\nfit <- gls(\n prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 +\n sin12 + cos12 + sin6 + cos6 + sin4 + cos4),\n data = d_its,\n correlation = corAR1(form = ~ 1 | age),\n weights = varIdent(form = ~ 1 | age)\n)\n\n\n\n\n\nThe Fourier terms (sin12, cos12, etc.) handle the recurring seasonal pattern, which is more stable than trying to absorb it through ARMA error terms (see https://otexts.com/fpp3/dhr.html (https://otexts.com/fpp3/dhr.html)).\n\n\n\n\nThe reference level is Adults, so coefficients are interpreted as:\n\n\n\n\n\ntime: pre-intervention slope for adults (prevalence per month)\n\n\n\n\nageChildren:time: additional slope for children;\n\n\n\n\n\nThe same logic applies for all ramp and step terms.\n\n\n\n\ncoef(fit) |>\n (\\(e) data.frame(\n Segment = c(\"Pre\", \"Post-1\", \"Post-2\", \"Post-3\"),\n Adults = round(cumsum(c(e[\"time\"],\n e[\"ramp1\"],\n e[\"ramp2\"],\n e[\"ramp3\"])), 3),\n Children = round(cumsum(c(e[\"time\"] + e[\"ageChildren:time\"],\n e[\"ramp1\"] + e[\"ageChildren:ramp1\"],\n e[\"ramp2\"] + e[\"ageChildren:ramp2\"],\n e[\"ramp3\"] + e[\"ageChildren:ramp3\"])), 3)\n))()\n\n#> Segment Adults Children\n#> time Pre 0.152 1.445\n#> ramp1 Post-1 0.919 9.276\n#> ramp2 Post-2 1.265 6.667\n#> ramp3 Post-3 2.387 8.418\n\n\n\n\n\npar(mfrow = c(1, 2))\nacf(residuals(fit)[d_its$age == \"Adults\"], main = \"Adults\")\nacf(residuals(fit)[d_its$age == \"Children\"], main = \"Children\")\ndev.off()\n\n\n\n\n\n[image: ; source: https://i.sstatic.net/2fnDCjiM.png]\n\n\n\n\nsummary(fit)\n\n#> Generalized least squares fit by REML\n#> Model: prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 + sin12 + cos12 + sin6 + cos6 + sin4 + cos4) \n#> Data: d_its \n#> AIC BIC logLik\n#> 1665.172 1769.226 -801.5858\n#> \n#> Correlation Structure: AR(1)\n#> Formula: ~1 | age \n#> Parameter estimate(s):\n#> Phi \n#> 0.1231768 \n#> Variance function:\n#> Structure: Different standard deviations per stratum\n#> Formula: ~1 | age \n#> Parameter estimates:\n#> Adults Children \n#> 1.000000 9.435418 \n#> \n#> Coefficients:\n#> Value Std.Error t-value p-value\n#> (Intercept) 10.25450 0.701853 14.610597 0.0000\n#> ageChildren 177.16318 6.659369 26.603599 0.0000\n#> time 0.15162 0.017799 8.518225 0.0000\n#> step1 1.83019 1.934456 0.946100 0.3452\n#> ramp1 0.76689 0.379858 2.018892 0.0448\n#> step2 -0.98456 2.681497 -0.367167 0.7139\n#> ramp2 0.34620 0.449034 0.770992 0.4416\n#> step3 -4.38832 2.094825 -2.094838 0.0374\n#> ramp3 1.12257 0.257109 4.366133 0.0000\n#> sin12 0.01845 0.399784 0.046145 0.9632\n#> cos12 1.16269 0.380402 3.056467 0.0025\n#> sin6 -0.70555 0.358820 -1.966309 0.0506\n#> cos6 -0.97079 0.355213 -2.732980 0.0068\n#> sin4 0.78337 0.333359 2.349941 0.0197\n#> cos4 0.05479 0.333865 0.164117 0.8698\n#> ageChildren:time 1.29381 0.168883 7.661002 0.0000\n#> ageChildren:step1 -1.02662 18.354630 -0.055933 0.9554\n#> ageChildren:ramp1 7.06407 3.604192 1.959960 0.0513\n#> ageChildren:step2 -35.82645 25.442751 -1.408120 0.1606\n#> ageChildren:ramp2 -2.95530 4.260553 -0.693643 0.4887\n#> ageChildren:step3 -29.49930 19.876247 -1.484148 0.1393\n#> ageChildren:ramp3 0.62770 2.439516 0.257306 0.7972\n#> ageChildren:sin12 4.22521 3.793256 1.113874 0.2666\n#> ageChildren:cos12 36.54005 3.609352 10.123715 0.0000\n#> ageChildren:sin6 -9.76953 3.404578 -2.869528 0.0045\n#> ageChildren:cos6 -25.08251 3.370352 -7.442104 0.0000\n#> ageChildren:sin4 13.68702 3.162997 4.327230 0.0000\n#> ageChildren:cos4 14.66062 3.167797 4.628016 0.0000\n#> \n#> Correlation: \n#> (Intr) agChld time step1 ramp1 step2 ramp2 step3 \n#> ageChildren -0.105 \n#> time -0.862 0.091 \n#> step1 0.173 -0.018 -0.309 \n#> ramp1 0.057 -0.006 -0.063 -0.763 \n#> step2 -0.021 0.002 0.020 0.366 -0.711 \n#> ramp2 -0.004 0.000 0.005 0.655 -0.831 0.306 \n#> step3 -0.015 0.002 0.015 0.007 -0.031 0.300 -0.403 \n#> ramp3 -0.016 0.002 0.016 0.008 -0.023 0.514 -0.520 0.649\n#> sin12 -0.056 0.006 0.043 0.155 -0.247 0.317 0.074 0.209\n#> cos12 -0.050 0.005 0.069 -0.188 0.137 -0.028 -0.155 0.065\n#> sin6 -0.008 0.001 -0.006 0.034 0.018 -0.103 0.052 -0.126\n#> cos6 -0.010 0.001 0.002 0.047 -0.026 -0.035 0.060 -0.071\n#> sin4 -0.024 0.002 0.028 -0.033 -0.004 0.059 -0.040 0.066\n#> cos4 -0.011 0.001 0.002 0.063 -0.078 0.095 0.024 0.069\n#> ageChildren:time 0.091 -0.862 -0.105 0.033 0.007 -0.002 0.000 -0.002\n#> ageChildren:step1 -0.018 0.173 0.033 -0.105 0.080 -0.039 -0.069 -0.001\n#> ageChildren:ramp1 -0.006 0.057 0.007 0.080 -0.105 0.075 0.088 0.003\n#> ageChildren:step2 0.002 -0.021 -0.002 -0.039 0.075 -0.105 -0.032 -0.032\n#> ageChildren:ramp2 0.000 -0.004 0.000 -0.069 0.088 -0.032 -0.105 0.043\n#> ageChildren:step3 0.002 -0.015 -0.002 -0.001 0.003 -0.032 0.043 -0.105\n#> ageChildren:ramp3 0.002 -0.016 -0.002 -0.001 0.002 -0.054 0.055 -0.068\n#> ageChildren:sin12 0.006 -0.056 -0.005 -0.016 0.026 -0.033 -0.008 -0.022\n#> ageChildren:cos12 0.005 -0.050 -0.007 0.020 -0.014 0.003 0.016 -0.007\n#> ageChildren:sin6 0.001 -0.008 0.001 -0.004 -0.002 0.011 -0.005 0.013\n#> ageChildren:cos6 0.001 -0.010 0.000 -0.005 0.003 0.004 -0.006 0.007\n#> ageChildren:sin4 0.002 -0.024 -0.003 0.003 0.000 -0.006 0.004 -0.007\n#> ageChildren:cos4 0.001 -0.011 0.000 -0.007 0.008 -0.010 -0.002 -0.007\n#> ramp3 sin12 cos12 sin6 cos6 sin4 cos4 agChl:\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 0.235 \n#> cos12 0.057 -0.008 \n#> sin6 -0.099 -0.036 -0.015 \n#> cos6 -0.058 -0.011 -0.017 0.017 \n#> sin4 0.074 0.025 0.014 -0.013 -0.006 \n#> cos4 0.073 0.041 -0.007 -0.009 -0.004 0.009 \n#> ageChildren:time -0.002 -0.005 -0.007 0.001 0.000 -0.003 0.000 \n#> ageChildren:step1 -0.001 -0.016 0.020 -0.004 -0.005 0.003 -0.007 -0.309\n#> ageChildren:ramp1 0.002 0.026 -0.014 -0.002 0.003 0.000 0.008 -0.063\n#> ageChildren:step2 -0.054 -0.033 0.003 0.011 0.004 -0.006 -0.010 0.020\n#> ageChildren:ramp2 0.055 -0.008 0.016 -0.005 -0.006 0.004 -0.002 0.005\n#> ageChildren:step3 -0.068 -0.022 -0.007 0.013 0.007 -0.007 -0.007 0.015\n#> ageChildren:ramp3 -0.105 -0.025 -0.006 0.010 0.006 -0.008 -0.008 0.016\n#> ageChildren:sin12 -0.025 -0.105 0.001 0.004 0.001 -0.003 -0.004 0.043\n#> ageChildren:cos12 -0.006 0.001 -0.105 0.002 0.002 -0.002 0.001 0.069\n#> ageChildren:sin6 0.010 0.004 0.002 -0.105 -0.002 0.001 0.001 -0.006\n#> ageChildren:cos6 0.006 0.001 0.002 -0.002 -0.105 0.001 0.000 0.002\n#> ageChildren:sin4 -0.008 -0.003 -0.002 0.001 0.001 -0.105 -0.001 0.028\n#> ageChildren:cos4 -0.008 -0.004 0.001 0.001 0.000 -0.001 -0.105 0.002\n#> agChldrn:s1 agChldrn:r1 agChldrn:s2 agChldrn:r2 agChldrn:s3\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 -0.763 \n#> ageChildren:step2 0.366 -0.711 \n#> ageChildren:ramp2 0.655 -0.831 0.306 \n#> ageChildren:step3 0.007 -0.031 0.300 -0.403 \n#> ageChildren:ramp3 0.008 -0.023 0.514 -0.520 0.649 \n#> ageChildren:sin12 0.155 -0.247 0.317 0.074 0.209 \n#> ageChildren:cos12 -0.188 0.137 -0.028 -0.155 0.065 \n#> ageChildren:sin6 0.034 0.018 -0.103 0.052 -0.126 \n#> ageChildren:cos6 0.047 -0.026 -0.035 0.060 -0.071 \n#> ageChildren:sin4 -0.033 -0.004 0.059 -0.040 0.066 \n#> ageChildren:cos4 0.063 -0.078 0.095 0.024 0.069 \n#> agChldrn:r3 agChldrn:s12 agChldrn:c12 agChldrn:s6 agChldrn:c6\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 \n#> ageChildren:step2 \n#> ageChildren:ramp2 \n#> ageChildren:step3 \n#> ageChildren:ramp3 \n#> ageChildren:sin12 0.235 \n#> ageChildren:cos12 0.057 -0.008 \n#> ageChildren:sin6 -0.099 -0.036 -0.015 \n#> ageChildren:cos6 -0.058 -0.011 -0.017 0.017 \n#> ageChildren:sin4 0.074 0.025 0.014 -0.013 -0.006 \n#> ageChildren:cos4 0.073 0.041 -0.007 -0.009 -0.004 \n#> agChldrn:s4\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 \n#> ageChildren:step2 \n#> ageChildren:ramp2 \n#> ageChildren:step3 \n#> ageChildren:ramp3 \n#> ageChildren:sin12 \n#> ageChildren:cos12 \n#> ageChildren:sin6 \n#> ageChildren:cos6 \n#> ageChildren:sin4 \n#> ageChildren:cos4 0.009 \n#> \n#> Standardized residuals:\n#> Min Q1 Med Q3 Max \n#> -5.25291094 -0.42240774 0.04331099 0.45224365 3.26323939 \n#> \n#> Residual standard error: 2.608572 \n#> Degrees of freedom: 240 total; 212 residual\n\n\n\n\n\nCreated on 2026-08-29 with reprex v2.1.1 (https://reprex.tidyverse.org)", "answer_url": "https://stats.stackexchange.com/a/676996", "author": "M--", "author_url": "https://stats.stackexchange.com/users/154449/m", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-29T05:35:19+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676979, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "M--", "profile_url": "https://stats.stackexchange.com/users/154449/m", "user_type": "registered"}, "created_at": "2026-08-29T05:35:19+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "4F437721-3A00-4B4D-BA06-A2F50516CC59", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/4F437721-3A00-4B4D-BA06-A2F50516CC59/view-source"}], "score": 3, "updated_at": "2026-08-29T05:35:19+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": 676996, "answers": [{"answer_html": "<p>Rather than fitting separate models, you can handle both age groups in a single <code>gls()</code> model with <code>age</code> interactions. This has two advantages over separate <code>auto.arima()</code> models: you get formal tests of whether intervention effects differ between groups via the interaction terms, and you can account for the fact that the two series have very different variances.</p>\n<p><code>age * (time + step1 + ramp1 + ...)</code> gives each group its own intercept, pre-intervention slope, and intervention effects, plus tests of whether they differ. Then, <code>corAR1(form = ~ 1 | age)</code> fits AR(1) autocorrelation within each group and <code>varIdent(form = ~ 1 | age)</code> allows different residual variances per group, which matters here since children's prevalence is an order of magnitude larger.</p>\n<pre class=\"lang-r prettyprint-override\"><code>library(nlme)\nlibrary(dplyr)\n\ninterventions &lt;- as.Date(c(&quot;2021-09-13&quot;, &quot;2022-06-02&quot;, &quot;2023-06-29&quot;))\n\nd_its &lt;- d |&gt;\n arrange(age, month) |&gt;\n mutate(.by = age,\n time = row_number() - 1,\n step1 = as.numeric(month &gt;= interventions[1]),\n ramp1 = ifelse(step1 == 1, cumsum(step1) - 1, 0),\n step2 = as.numeric(month &gt;= interventions[2]),\n ramp2 = ifelse(step2 == 1, cumsum(step2) - 1, 0),\n step3 = as.numeric(month &gt;= interventions[3]),\n ramp3 = ifelse(step3 == 1, cumsum(step3) - 1, 0),\n sin12 = sin(2 * pi * time / 12),\n cos12 = cos(2 * pi * time / 12),\n sin6 = sin(2 * pi * time / 6),\n cos6 = cos(2 * pi * time / 6),\n sin4 = sin(2 * pi * time / 4),\n cos4 = cos(2 * pi * time / 4)\n ) |&gt;\n mutate(age = factor(age))\n\nfit &lt;- gls(\n prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 +\n sin12 + cos12 + sin6 + cos6 + sin4 + cos4),\n data = d_its,\n correlation = corAR1(form = ~ 1 | age),\n weights = varIdent(form = ~ 1 | age)\n)\n</code></pre>\n<p>The Fourier terms (<code>sin12</code>, <code>cos12</code>, etc.) handle the recurring seasonal pattern, which is more stable than trying to absorb it through ARMA error terms (see <a href=\"https://otexts.com/fpp3/dhr.html\" rel=\"nofollow noreferrer\">https://otexts.com/fpp3/dhr.html</a>).</p>\n<p>The reference level is Adults, so coefficients are interpreted as:</p>\n<ul>\n<li><code>time</code>: pre-intervention slope for adults (prevalence per month)</li>\n<li><code>ageChildren:time</code>: <em>additional</em> slope for children;</li>\n</ul>\n<p>The same logic applies for all <code>ramp</code> and <code>step</code> terms.</p>\n<pre class=\"lang-r prettyprint-override\"><code>coef(fit) |&gt;\n (\\(e) data.frame(\n Segment = c(&quot;Pre&quot;, &quot;Post-1&quot;, &quot;Post-2&quot;, &quot;Post-3&quot;),\n Adults = round(cumsum(c(e[&quot;time&quot;],\n e[&quot;ramp1&quot;],\n e[&quot;ramp2&quot;],\n e[&quot;ramp3&quot;])), 3),\n Children = round(cumsum(c(e[&quot;time&quot;] + e[&quot;ageChildren:time&quot;],\n e[&quot;ramp1&quot;] + e[&quot;ageChildren:ramp1&quot;],\n e[&quot;ramp2&quot;] + e[&quot;ageChildren:ramp2&quot;],\n e[&quot;ramp3&quot;] + e[&quot;ageChildren:ramp3&quot;])), 3)\n))()\n\n#&gt; Segment Adults Children\n#&gt; time Pre 0.152 1.445\n#&gt; ramp1 Post-1 0.919 9.276\n#&gt; ramp2 Post-2 1.265 6.667\n#&gt; ramp3 Post-3 2.387 8.418\n</code></pre>\n<pre class=\"lang-r prettyprint-override\"><code>par(mfrow = c(1, 2))\nacf(residuals(fit)[d_its<span class=\"math-container\">$age == \"Adults\"], main = \"Adults\")\nacf(residuals(fit)[d_its$</span>age == &quot;Children&quot;], main = &quot;Children&quot;)\ndev.off()\n</code></pre>\n<p><img src=\"https://i.sstatic.net/2fnDCjiM.png\" alt=\"\" /></p>\n<pre class=\"lang-r prettyprint-override\"><code>summary(fit)\n\n#&gt; Generalized least squares fit by REML\n#&gt; Model: prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 + sin12 + cos12 + sin6 + cos6 + sin4 + cos4) \n#&gt; Data: d_its \n#&gt; AIC BIC logLik\n#&gt; 1665.172 1769.226 -801.5858\n#&gt; \n#&gt; Correlation Structure: AR(1)\n#&gt; Formula: ~1 | age \n#&gt; Parameter estimate(s):\n#&gt; Phi \n#&gt; 0.1231768 \n#&gt; Variance function:\n#&gt; Structure: Different standard deviations per stratum\n#&gt; Formula: ~1 | age \n#&gt; Parameter estimates:\n#&gt; Adults Children \n#&gt; 1.000000 9.435418 \n#&gt; \n#&gt; Coefficients:\n#&gt; Value Std.Error t-value p-value\n#&gt; (Intercept) 10.25450 0.701853 14.610597 0.0000\n#&gt; ageChildren 177.16318 6.659369 26.603599 0.0000\n#&gt; time 0.15162 0.017799 8.518225 0.0000\n#&gt; step1 1.83019 1.934456 0.946100 0.3452\n#&gt; ramp1 0.76689 0.379858 2.018892 0.0448\n#&gt; step2 -0.98456 2.681497 -0.367167 0.7139\n#&gt; ramp2 0.34620 0.449034 0.770992 0.4416\n#&gt; step3 -4.38832 2.094825 -2.094838 0.0374\n#&gt; ramp3 1.12257 0.257109 4.366133 0.0000\n#&gt; sin12 0.01845 0.399784 0.046145 0.9632\n#&gt; cos12 1.16269 0.380402 3.056467 0.0025\n#&gt; sin6 -0.70555 0.358820 -1.966309 0.0506\n#&gt; cos6 -0.97079 0.355213 -2.732980 0.0068\n#&gt; sin4 0.78337 0.333359 2.349941 0.0197\n#&gt; cos4 0.05479 0.333865 0.164117 0.8698\n#&gt; ageChildren:time 1.29381 0.168883 7.661002 0.0000\n#&gt; ageChildren:step1 -1.02662 18.354630 -0.055933 0.9554\n#&gt; ageChildren:ramp1 7.06407 3.604192 1.959960 0.0513\n#&gt; ageChildren:step2 -35.82645 25.442751 -1.408120 0.1606\n#&gt; ageChildren:ramp2 -2.95530 4.260553 -0.693643 0.4887\n#&gt; ageChildren:step3 -29.49930 19.876247 -1.484148 0.1393\n#&gt; ageChildren:ramp3 0.62770 2.439516 0.257306 0.7972\n#&gt; ageChildren:sin12 4.22521 3.793256 1.113874 0.2666\n#&gt; ageChildren:cos12 36.54005 3.609352 10.123715 0.0000\n#&gt; ageChildren:sin6 -9.76953 3.404578 -2.869528 0.0045\n#&gt; ageChildren:cos6 -25.08251 3.370352 -7.442104 0.0000\n#&gt; ageChildren:sin4 13.68702 3.162997 4.327230 0.0000\n#&gt; ageChildren:cos4 14.66062 3.167797 4.628016 0.0000\n#&gt; \n#&gt; Correlation: \n#&gt; (Intr) agChld time step1 ramp1 step2 ramp2 step3 \n#&gt; ageChildren -0.105 \n#&gt; time -0.862 0.091 \n#&gt; step1 0.173 -0.018 -0.309 \n#&gt; ramp1 0.057 -0.006 -0.063 -0.763 \n#&gt; step2 -0.021 0.002 0.020 0.366 -0.711 \n#&gt; ramp2 -0.004 0.000 0.005 0.655 -0.831 0.306 \n#&gt; step3 -0.015 0.002 0.015 0.007 -0.031 0.300 -0.403 \n#&gt; ramp3 -0.016 0.002 0.016 0.008 -0.023 0.514 -0.520 0.649\n#&gt; sin12 -0.056 0.006 0.043 0.155 -0.247 0.317 0.074 0.209\n#&gt; cos12 -0.050 0.005 0.069 -0.188 0.137 -0.028 -0.155 0.065\n#&gt; sin6 -0.008 0.001 -0.006 0.034 0.018 -0.103 0.052 -0.126\n#&gt; cos6 -0.010 0.001 0.002 0.047 -0.026 -0.035 0.060 -0.071\n#&gt; sin4 -0.024 0.002 0.028 -0.033 -0.004 0.059 -0.040 0.066\n#&gt; cos4 -0.011 0.001 0.002 0.063 -0.078 0.095 0.024 0.069\n#&gt; ageChildren:time 0.091 -0.862 -0.105 0.033 0.007 -0.002 0.000 -0.002\n#&gt; ageChildren:step1 -0.018 0.173 0.033 -0.105 0.080 -0.039 -0.069 -0.001\n#&gt; ageChildren:ramp1 -0.006 0.057 0.007 0.080 -0.105 0.075 0.088 0.003\n#&gt; ageChildren:step2 0.002 -0.021 -0.002 -0.039 0.075 -0.105 -0.032 -0.032\n#&gt; ageChildren:ramp2 0.000 -0.004 0.000 -0.069 0.088 -0.032 -0.105 0.043\n#&gt; ageChildren:step3 0.002 -0.015 -0.002 -0.001 0.003 -0.032 0.043 -0.105\n#&gt; ageChildren:ramp3 0.002 -0.016 -0.002 -0.001 0.002 -0.054 0.055 -0.068\n#&gt; ageChildren:sin12 0.006 -0.056 -0.005 -0.016 0.026 -0.033 -0.008 -0.022\n#&gt; ageChildren:cos12 0.005 -0.050 -0.007 0.020 -0.014 0.003 0.016 -0.007\n#&gt; ageChildren:sin6 0.001 -0.008 0.001 -0.004 -0.002 0.011 -0.005 0.013\n#&gt; ageChildren:cos6 0.001 -0.010 0.000 -0.005 0.003 0.004 -0.006 0.007\n#&gt; ageChildren:sin4 0.002 -0.024 -0.003 0.003 0.000 -0.006 0.004 -0.007\n#&gt; ageChildren:cos4 0.001 -0.011 0.000 -0.007 0.008 -0.010 -0.002 -0.007\n#&gt; ramp3 sin12 cos12 sin6 cos6 sin4 cos4 agChl:\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 0.235 \n#&gt; cos12 0.057 -0.008 \n#&gt; sin6 -0.099 -0.036 -0.015 \n#&gt; cos6 -0.058 -0.011 -0.017 0.017 \n#&gt; sin4 0.074 0.025 0.014 -0.013 -0.006 \n#&gt; cos4 0.073 0.041 -0.007 -0.009 -0.004 0.009 \n#&gt; ageChildren:time -0.002 -0.005 -0.007 0.001 0.000 -0.003 0.000 \n#&gt; ageChildren:step1 -0.001 -0.016 0.020 -0.004 -0.005 0.003 -0.007 -0.309\n#&gt; ageChildren:ramp1 0.002 0.026 -0.014 -0.002 0.003 0.000 0.008 -0.063\n#&gt; ageChildren:step2 -0.054 -0.033 0.003 0.011 0.004 -0.006 -0.010 0.020\n#&gt; ageChildren:ramp2 0.055 -0.008 0.016 -0.005 -0.006 0.004 -0.002 0.005\n#&gt; ageChildren:step3 -0.068 -0.022 -0.007 0.013 0.007 -0.007 -0.007 0.015\n#&gt; ageChildren:ramp3 -0.105 -0.025 -0.006 0.010 0.006 -0.008 -0.008 0.016\n#&gt; ageChildren:sin12 -0.025 -0.105 0.001 0.004 0.001 -0.003 -0.004 0.043\n#&gt; ageChildren:cos12 -0.006 0.001 -0.105 0.002 0.002 -0.002 0.001 0.069\n#&gt; ageChildren:sin6 0.010 0.004 0.002 -0.105 -0.002 0.001 0.001 -0.006\n#&gt; ageChildren:cos6 0.006 0.001 0.002 -0.002 -0.105 0.001 0.000 0.002\n#&gt; ageChildren:sin4 -0.008 -0.003 -0.002 0.001 0.001 -0.105 -0.001 0.028\n#&gt; ageChildren:cos4 -0.008 -0.004 0.001 0.001 0.000 -0.001 -0.105 0.002\n#&gt; agChldrn:s1 agChldrn:r1 agChldrn:s2 agChldrn:r2 agChldrn:s3\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 -0.763 \n#&gt; ageChildren:step2 0.366 -0.711 \n#&gt; ageChildren:ramp2 0.655 -0.831 0.306 \n#&gt; ageChildren:step3 0.007 -0.031 0.300 -0.403 \n#&gt; ageChildren:ramp3 0.008 -0.023 0.514 -0.520 0.649 \n#&gt; ageChildren:sin12 0.155 -0.247 0.317 0.074 0.209 \n#&gt; ageChildren:cos12 -0.188 0.137 -0.028 -0.155 0.065 \n#&gt; ageChildren:sin6 0.034 0.018 -0.103 0.052 -0.126 \n#&gt; ageChildren:cos6 0.047 -0.026 -0.035 0.060 -0.071 \n#&gt; ageChildren:sin4 -0.033 -0.004 0.059 -0.040 0.066 \n#&gt; ageChildren:cos4 0.063 -0.078 0.095 0.024 0.069 \n#&gt; agChldrn:r3 agChldrn:s12 agChldrn:c12 agChldrn:s6 agChldrn:c6\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 \n#&gt; ageChildren:step2 \n#&gt; ageChildren:ramp2 \n#&gt; ageChildren:step3 \n#&gt; ageChildren:ramp3 \n#&gt; ageChildren:sin12 0.235 \n#&gt; ageChildren:cos12 0.057 -0.008 \n#&gt; ageChildren:sin6 -0.099 -0.036 -0.015 \n#&gt; ageChildren:cos6 -0.058 -0.011 -0.017 0.017 \n#&gt; ageChildren:sin4 0.074 0.025 0.014 -0.013 -0.006 \n#&gt; ageChildren:cos4 0.073 0.041 -0.007 -0.009 -0.004 \n#&gt; agChldrn:s4\n#&gt; ageChildren \n#&gt; time \n#&gt; step1 \n#&gt; ramp1 \n#&gt; step2 \n#&gt; ramp2 \n#&gt; step3 \n#&gt; ramp3 \n#&gt; sin12 \n#&gt; cos12 \n#&gt; sin6 \n#&gt; cos6 \n#&gt; sin4 \n#&gt; cos4 \n#&gt; ageChildren:time \n#&gt; ageChildren:step1 \n#&gt; ageChildren:ramp1 \n#&gt; ageChildren:step2 \n#&gt; ageChildren:ramp2 \n#&gt; ageChildren:step3 \n#&gt; ageChildren:ramp3 \n#&gt; ageChildren:sin12 \n#&gt; ageChildren:cos12 \n#&gt; ageChildren:sin6 \n#&gt; ageChildren:cos6 \n#&gt; ageChildren:sin4 \n#&gt; ageChildren:cos4 0.009 \n#&gt; \n#&gt; Standardized residuals:\n#&gt; Min Q1 Med Q3 Max \n#&gt; -5.25291094 -0.42240774 0.04331099 0.45224365 3.26323939 \n#&gt; \n#&gt; Residual standard error: 2.608572 \n#&gt; Degrees of freedom: 240 total; 212 residual\n</code></pre>\n<p><sup>Created on 2026-08-29 with <a href=\"https://reprex.tidyverse.org\" rel=\"nofollow noreferrer\">reprex v2.1.1</a></sup></p>\n", "answer_id": 676996, "answer_text": "Rather than fitting separate models, you can handle both age groups in a single gls() model with age interactions. This has two advantages over separate auto.arima() models: you get formal tests of whether intervention effects differ between groups via the interaction terms, and you can account for the fact that the two series have very different variances.\n\n\n\n\nage * (time + step1 + ramp1 + ...) gives each group its own intercept, pre-intervention slope, and intervention effects, plus tests of whether they differ. Then, corAR1(form = ~ 1 | age) fits AR(1) autocorrelation within each group and varIdent(form = ~ 1 | age) allows different residual variances per group, which matters here since children's prevalence is an order of magnitude larger.\n\n\n\n\nlibrary(nlme)\nlibrary(dplyr)\n\ninterventions <- as.Date(c(\"2021-09-13\", \"2022-06-02\", \"2023-06-29\"))\n\nd_its <- d |>\n arrange(age, month) |>\n mutate(.by = age,\n time = row_number() - 1,\n step1 = as.numeric(month >= interventions[1]),\n ramp1 = ifelse(step1 == 1, cumsum(step1) - 1, 0),\n step2 = as.numeric(month >= interventions[2]),\n ramp2 = ifelse(step2 == 1, cumsum(step2) - 1, 0),\n step3 = as.numeric(month >= interventions[3]),\n ramp3 = ifelse(step3 == 1, cumsum(step3) - 1, 0),\n sin12 = sin(2 * pi * time / 12),\n cos12 = cos(2 * pi * time / 12),\n sin6 = sin(2 * pi * time / 6),\n cos6 = cos(2 * pi * time / 6),\n sin4 = sin(2 * pi * time / 4),\n cos4 = cos(2 * pi * time / 4)\n ) |>\n mutate(age = factor(age))\n\nfit <- gls(\n prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 +\n sin12 + cos12 + sin6 + cos6 + sin4 + cos4),\n data = d_its,\n correlation = corAR1(form = ~ 1 | age),\n weights = varIdent(form = ~ 1 | age)\n)\n\n\n\n\n\nThe Fourier terms (sin12, cos12, etc.) handle the recurring seasonal pattern, which is more stable than trying to absorb it through ARMA error terms (see https://otexts.com/fpp3/dhr.html (https://otexts.com/fpp3/dhr.html)).\n\n\n\n\nThe reference level is Adults, so coefficients are interpreted as:\n\n\n\n\n\ntime: pre-intervention slope for adults (prevalence per month)\n\n\n\n\nageChildren:time: additional slope for children;\n\n\n\n\n\nThe same logic applies for all ramp and step terms.\n\n\n\n\ncoef(fit) |>\n (\\(e) data.frame(\n Segment = c(\"Pre\", \"Post-1\", \"Post-2\", \"Post-3\"),\n Adults = round(cumsum(c(e[\"time\"],\n e[\"ramp1\"],\n e[\"ramp2\"],\n e[\"ramp3\"])), 3),\n Children = round(cumsum(c(e[\"time\"] + e[\"ageChildren:time\"],\n e[\"ramp1\"] + e[\"ageChildren:ramp1\"],\n e[\"ramp2\"] + e[\"ageChildren:ramp2\"],\n e[\"ramp3\"] + e[\"ageChildren:ramp3\"])), 3)\n))()\n\n#> Segment Adults Children\n#> time Pre 0.152 1.445\n#> ramp1 Post-1 0.919 9.276\n#> ramp2 Post-2 1.265 6.667\n#> ramp3 Post-3 2.387 8.418\n\n\n\n\n\npar(mfrow = c(1, 2))\nacf(residuals(fit)[d_its$age == \"Adults\"], main = \"Adults\")\nacf(residuals(fit)[d_its$age == \"Children\"], main = \"Children\")\ndev.off()\n\n\n\n\n\n[image: ; source: https://i.sstatic.net/2fnDCjiM.png]\n\n\n\n\nsummary(fit)\n\n#> Generalized least squares fit by REML\n#> Model: prevalence ~ age * (time + step1 + ramp1 + step2 + ramp2 + step3 + ramp3 + sin12 + cos12 + sin6 + cos6 + sin4 + cos4) \n#> Data: d_its \n#> AIC BIC logLik\n#> 1665.172 1769.226 -801.5858\n#> \n#> Correlation Structure: AR(1)\n#> Formula: ~1 | age \n#> Parameter estimate(s):\n#> Phi \n#> 0.1231768 \n#> Variance function:\n#> Structure: Different standard deviations per stratum\n#> Formula: ~1 | age \n#> Parameter estimates:\n#> Adults Children \n#> 1.000000 9.435418 \n#> \n#> Coefficients:\n#> Value Std.Error t-value p-value\n#> (Intercept) 10.25450 0.701853 14.610597 0.0000\n#> ageChildren 177.16318 6.659369 26.603599 0.0000\n#> time 0.15162 0.017799 8.518225 0.0000\n#> step1 1.83019 1.934456 0.946100 0.3452\n#> ramp1 0.76689 0.379858 2.018892 0.0448\n#> step2 -0.98456 2.681497 -0.367167 0.7139\n#> ramp2 0.34620 0.449034 0.770992 0.4416\n#> step3 -4.38832 2.094825 -2.094838 0.0374\n#> ramp3 1.12257 0.257109 4.366133 0.0000\n#> sin12 0.01845 0.399784 0.046145 0.9632\n#> cos12 1.16269 0.380402 3.056467 0.0025\n#> sin6 -0.70555 0.358820 -1.966309 0.0506\n#> cos6 -0.97079 0.355213 -2.732980 0.0068\n#> sin4 0.78337 0.333359 2.349941 0.0197\n#> cos4 0.05479 0.333865 0.164117 0.8698\n#> ageChildren:time 1.29381 0.168883 7.661002 0.0000\n#> ageChildren:step1 -1.02662 18.354630 -0.055933 0.9554\n#> ageChildren:ramp1 7.06407 3.604192 1.959960 0.0513\n#> ageChildren:step2 -35.82645 25.442751 -1.408120 0.1606\n#> ageChildren:ramp2 -2.95530 4.260553 -0.693643 0.4887\n#> ageChildren:step3 -29.49930 19.876247 -1.484148 0.1393\n#> ageChildren:ramp3 0.62770 2.439516 0.257306 0.7972\n#> ageChildren:sin12 4.22521 3.793256 1.113874 0.2666\n#> ageChildren:cos12 36.54005 3.609352 10.123715 0.0000\n#> ageChildren:sin6 -9.76953 3.404578 -2.869528 0.0045\n#> ageChildren:cos6 -25.08251 3.370352 -7.442104 0.0000\n#> ageChildren:sin4 13.68702 3.162997 4.327230 0.0000\n#> ageChildren:cos4 14.66062 3.167797 4.628016 0.0000\n#> \n#> Correlation: \n#> (Intr) agChld time step1 ramp1 step2 ramp2 step3 \n#> ageChildren -0.105 \n#> time -0.862 0.091 \n#> step1 0.173 -0.018 -0.309 \n#> ramp1 0.057 -0.006 -0.063 -0.763 \n#> step2 -0.021 0.002 0.020 0.366 -0.711 \n#> ramp2 -0.004 0.000 0.005 0.655 -0.831 0.306 \n#> step3 -0.015 0.002 0.015 0.007 -0.031 0.300 -0.403 \n#> ramp3 -0.016 0.002 0.016 0.008 -0.023 0.514 -0.520 0.649\n#> sin12 -0.056 0.006 0.043 0.155 -0.247 0.317 0.074 0.209\n#> cos12 -0.050 0.005 0.069 -0.188 0.137 -0.028 -0.155 0.065\n#> sin6 -0.008 0.001 -0.006 0.034 0.018 -0.103 0.052 -0.126\n#> cos6 -0.010 0.001 0.002 0.047 -0.026 -0.035 0.060 -0.071\n#> sin4 -0.024 0.002 0.028 -0.033 -0.004 0.059 -0.040 0.066\n#> cos4 -0.011 0.001 0.002 0.063 -0.078 0.095 0.024 0.069\n#> ageChildren:time 0.091 -0.862 -0.105 0.033 0.007 -0.002 0.000 -0.002\n#> ageChildren:step1 -0.018 0.173 0.033 -0.105 0.080 -0.039 -0.069 -0.001\n#> ageChildren:ramp1 -0.006 0.057 0.007 0.080 -0.105 0.075 0.088 0.003\n#> ageChildren:step2 0.002 -0.021 -0.002 -0.039 0.075 -0.105 -0.032 -0.032\n#> ageChildren:ramp2 0.000 -0.004 0.000 -0.069 0.088 -0.032 -0.105 0.043\n#> ageChildren:step3 0.002 -0.015 -0.002 -0.001 0.003 -0.032 0.043 -0.105\n#> ageChildren:ramp3 0.002 -0.016 -0.002 -0.001 0.002 -0.054 0.055 -0.068\n#> ageChildren:sin12 0.006 -0.056 -0.005 -0.016 0.026 -0.033 -0.008 -0.022\n#> ageChildren:cos12 0.005 -0.050 -0.007 0.020 -0.014 0.003 0.016 -0.007\n#> ageChildren:sin6 0.001 -0.008 0.001 -0.004 -0.002 0.011 -0.005 0.013\n#> ageChildren:cos6 0.001 -0.010 0.000 -0.005 0.003 0.004 -0.006 0.007\n#> ageChildren:sin4 0.002 -0.024 -0.003 0.003 0.000 -0.006 0.004 -0.007\n#> ageChildren:cos4 0.001 -0.011 0.000 -0.007 0.008 -0.010 -0.002 -0.007\n#> ramp3 sin12 cos12 sin6 cos6 sin4 cos4 agChl:\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 0.235 \n#> cos12 0.057 -0.008 \n#> sin6 -0.099 -0.036 -0.015 \n#> cos6 -0.058 -0.011 -0.017 0.017 \n#> sin4 0.074 0.025 0.014 -0.013 -0.006 \n#> cos4 0.073 0.041 -0.007 -0.009 -0.004 0.009 \n#> ageChildren:time -0.002 -0.005 -0.007 0.001 0.000 -0.003 0.000 \n#> ageChildren:step1 -0.001 -0.016 0.020 -0.004 -0.005 0.003 -0.007 -0.309\n#> ageChildren:ramp1 0.002 0.026 -0.014 -0.002 0.003 0.000 0.008 -0.063\n#> ageChildren:step2 -0.054 -0.033 0.003 0.011 0.004 -0.006 -0.010 0.020\n#> ageChildren:ramp2 0.055 -0.008 0.016 -0.005 -0.006 0.004 -0.002 0.005\n#> ageChildren:step3 -0.068 -0.022 -0.007 0.013 0.007 -0.007 -0.007 0.015\n#> ageChildren:ramp3 -0.105 -0.025 -0.006 0.010 0.006 -0.008 -0.008 0.016\n#> ageChildren:sin12 -0.025 -0.105 0.001 0.004 0.001 -0.003 -0.004 0.043\n#> ageChildren:cos12 -0.006 0.001 -0.105 0.002 0.002 -0.002 0.001 0.069\n#> ageChildren:sin6 0.010 0.004 0.002 -0.105 -0.002 0.001 0.001 -0.006\n#> ageChildren:cos6 0.006 0.001 0.002 -0.002 -0.105 0.001 0.000 0.002\n#> ageChildren:sin4 -0.008 -0.003 -0.002 0.001 0.001 -0.105 -0.001 0.028\n#> ageChildren:cos4 -0.008 -0.004 0.001 0.001 0.000 -0.001 -0.105 0.002\n#> agChldrn:s1 agChldrn:r1 agChldrn:s2 agChldrn:r2 agChldrn:s3\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 -0.763 \n#> ageChildren:step2 0.366 -0.711 \n#> ageChildren:ramp2 0.655 -0.831 0.306 \n#> ageChildren:step3 0.007 -0.031 0.300 -0.403 \n#> ageChildren:ramp3 0.008 -0.023 0.514 -0.520 0.649 \n#> ageChildren:sin12 0.155 -0.247 0.317 0.074 0.209 \n#> ageChildren:cos12 -0.188 0.137 -0.028 -0.155 0.065 \n#> ageChildren:sin6 0.034 0.018 -0.103 0.052 -0.126 \n#> ageChildren:cos6 0.047 -0.026 -0.035 0.060 -0.071 \n#> ageChildren:sin4 -0.033 -0.004 0.059 -0.040 0.066 \n#> ageChildren:cos4 0.063 -0.078 0.095 0.024 0.069 \n#> agChldrn:r3 agChldrn:s12 agChldrn:c12 agChldrn:s6 agChldrn:c6\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 \n#> ageChildren:step2 \n#> ageChildren:ramp2 \n#> ageChildren:step3 \n#> ageChildren:ramp3 \n#> ageChildren:sin12 0.235 \n#> ageChildren:cos12 0.057 -0.008 \n#> ageChildren:sin6 -0.099 -0.036 -0.015 \n#> ageChildren:cos6 -0.058 -0.011 -0.017 0.017 \n#> ageChildren:sin4 0.074 0.025 0.014 -0.013 -0.006 \n#> ageChildren:cos4 0.073 0.041 -0.007 -0.009 -0.004 \n#> agChldrn:s4\n#> ageChildren \n#> time \n#> step1 \n#> ramp1 \n#> step2 \n#> ramp2 \n#> step3 \n#> ramp3 \n#> sin12 \n#> cos12 \n#> sin6 \n#> cos6 \n#> sin4 \n#> cos4 \n#> ageChildren:time \n#> ageChildren:step1 \n#> ageChildren:ramp1 \n#> ageChildren:step2 \n#> ageChildren:ramp2 \n#> ageChildren:step3 \n#> ageChildren:ramp3 \n#> ageChildren:sin12 \n#> ageChildren:cos12 \n#> ageChildren:sin6 \n#> ageChildren:cos6 \n#> ageChildren:sin4 \n#> ageChildren:cos4 0.009 \n#> \n#> Standardized residuals:\n#> Min Q1 Med Q3 Max \n#> -5.25291094 -0.42240774 0.04331099 0.45224365 3.26323939 \n#> \n#> Residual standard error: 2.608572 \n#> Degrees of freedom: 240 total; 212 residual\n\n\n\n\n\nCreated on 2026-08-29 with reprex v2.1.1 (https://reprex.tidyverse.org)", "answer_url": "https://stats.stackexchange.com/a/676996", "author": "M--", "author_url": "https://stats.stackexchange.com/users/154449/m", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-29T05:35:19+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676979, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "M--", "profile_url": "https://stats.stackexchange.com/users/154449/m", "user_type": "registered"}, "created_at": "2026-08-29T05:35:19+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "4F437721-3A00-4B4D-BA06-A2F50516CC59", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/4F437721-3A00-4B4D-BA06-A2F50516CC59/view-source"}], "score": 3, "updated_at": "2026-08-29T05:35:19+00:00"}], "domain": "statistics", "external_links": ["https://doi.org/10.1093/ije/dyaa148", "https://doi.org/10.1186/s12874-021-01235-8", "https://doi.org/10.1186/s12889-017-4998-9", "https://doi.org/10.3111/13696998.2011.626097", "https://i.sstatic.net/2fnDCjiM.png", "https://i.sstatic.net/4a0pdydL.png", "https://i.sstatic.net/pzs11NTf.png", "https://otexts.com/fpp3/dhr.html", "https://reprex.tidyverse.org", "https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/gls.html"], "medical_sensitive": true, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "Thomas", "question_author_url": "https://stats.stackexchange.com/users/263046/thomas", "question_author_user_type": "registered", "question_created_at": "2026-08-27T20:48:18+00:00", "question_html": "<h2>Background</h2>\n<ul>\n<li>Data: time series of the monthly prevalence of patients receiving a given medication by age group (children or adults) from January 2016 to December 2026</li>\n<li>Interventions: three successive regulatory interventions implemented on September 2021, June 2022, and June 2023 to facilitate prescription of the medication (dotted lines on the figure below)</li>\n<li>Aim: to estimate pre-intervention slope, post-intervention 1 slope, post-intervention 2 slope, and post-intervention 3 slope depending on age group to assess the impact of the interventions (i.e., interrupted time series analysis, ITS)</li>\n</ul>\n<pre class=\"lang-r prettyprint-override\"><code>month &lt;- seq(\n from = as.Date(&quot;2016-01-01&quot;),\n to = as.Date(&quot;2025-12-31&quot;),\n by = &quot;month&quot;\n)\nmonth &lt;- rep(month, each = 2)\nage &lt;- rep(c(&quot;Children&quot;, &quot;Adults&quot;), 120)\nprevalence &lt;- c(\n 214.1, 11.3, 194.9, 11.1, 220.3, 11.8, 200.7, 11.7, 213.5, \n 11.7, 197.6, 11.5, 128.2, 10.3, 138.6, 9.9, 215, 12.1, 201.9, \n 11.9, 223.8, 12.2, 204.9, 12.4, 234.1, 12.7, 202, 11.9, 247, \n 13.6, 209.8, 13, 236.7, 13.4, 216.9, 13.1, 145.1, 12, 144.3, \n 11, 235.1, 13.7, 227.5, 13.9, 242.5, 13.9, 229.5, 13.8, 255.3, \n 14.5, 226.3, 13.6, 260.8, 15.2, 229.7, 14.2, 245.5, 14.7, 232.6, \n 14.4, 158.8, 13.4, 165, 12.5, 244.3, 14.8, 249.4, 15.7, 259.4, \n 15.5, 244, 15.3, 275.7, 16.2, 240.7, 14.9, 274.4, 16.2, 256.8, \n 16.3, 274.3, 16.4, 240.3, 15.5, 178.2, 15.5, 180.3, 13.6, 264, \n 16.3, 271.3, 17.4, 277.8, 16.9, 261.5, 16.8, 296.9, 17.8, 265.8, \n 17.2, 270.4, 17.9, 218.8, 16, 214.7, 16, 221.8, 16.7, 183.6, \n 16.8, 185.4, 14.9, 273.6, 18.7, 276.7, 19.3, 285, 19.4, 282.8, \n 19.9, 308.3, 20.2, 279.2, 19.3, 331.1, 22, 289.3, 21.2, 303, \n 20.9, 307.5, 21.8, 221.2, 20.9, 215.4, 18.9, 324, 23.4, 323.3, \n 24.3, 328.3, 24.7, 326.5, 25.8, 349.2, 26.1, 319.1, 25.5, 379.3, \n 29, 341.7, 28.3, 365.1, 28.6, 351.7, 30.1, 253.7, 28.6, 266.9, \n 27, 375, 33.4, 373.5, 34.4, 385.8, 35.5, 368.1, 36, 413.7, 38.8, \n 376, 37.4, 440.1, 42.5, 390.3, 40.8, 409.5, 42.6, 402, 44.9, \n 297.9, 43.4, 297.1, 40.2, 430.9, 49.3, 433.4, 51.2, 454.1, 53.1, \n 441.4, 53.4, 472.9, 57, 455.8, 57, 496.3, 62.6, 475, 63.1, 483.4, \n 63.3, 466, 65.1, 367.3, 66.2, 368.4, 58.6, 499.4, 70.8, 528.3, \n 76.1, 533.8, 76, 525.6, 77.2, 588.3, 84.8, 532.4, 81.1, 599.6, \n 90.9, 580.3, 90.8, 601.5, 93.8, 560.6, 95.1, 455.2, 96.9, 440.6, \n 83.7, 629.2, 107.7, 655.7, 112.8, 653.8, 113.2, 674.9, 118.7\n)\nd &lt;- data.frame(month, age, prevalence)\n\npar(mfrow = c(1, 2))\nplot(prevalence ~ month, d, type = &quot;l&quot;, main = &quot;Children&quot;, subset = age == &quot;Children&quot;)\nplot(prevalence ~ month, d, type = &quot;l&quot;, main = &quot;Adults&quot;, subset = age == &quot;Adults&quot;)\ndev.off()\n</code></pre>\n<p><a href=\"https://i.sstatic.net/4a0pdydL.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/4a0pdydL.png\" alt=\"time series\" /></a></p>\n<h2>What I did</h2>\n<p>I started with children.</p>\n<p>I constructed the ITS variables for the 3 interventions (<code>step1</code>, <code>ramp1</code>, <code>step2</code>, <code>ramp2</code>, <code>step3</code>, <code>ramp3</code>) following <a href=\"https://doi.org/10.1186/s12874-021-01235-8\" rel=\"nofollow noreferrer\">Schaffer et al.</a> and <a href=\"https://doi.org/10.1093/ije/dyaa148\" rel=\"nofollow noreferrer\">Xiao et al.</a></p>\n<pre class=\"lang-r prettyprint-override\"><code>d2 &lt;- subset(d, age == &quot;Children&quot;)\nd2<span class=\"math-container\">$prevalence &lt;- ts(d2$</span>prevalence, start = 2016, frequency = 12)\n\ninterventions &lt;- as.Date(c(&quot;2021-09-13&quot;, &quot;2022-06-02&quot;, &quot;2023-06-29&quot;))\n\nstep1 &lt;- as.numeric(d2$month &gt;= interventions[1])\nramp1 &lt;- ifelse(step1 == 1, cumsum(step1) - 1, 0)\n\nstep2 &lt;- as.numeric(d2$month &gt;= interventions[2])\nramp2 &lt;- ifelse(step2 == 1, cumsum(step2) - 1, 0)\n\nstep3 &lt;- as.numeric(d2$month &gt;= interventions[3])\nramp3 &lt;- ifelse(step3 == 1, cumsum(step3) - 1, 0)\n\ntime &lt;- seq(from = 0, along.with = d2$month)\n\nxreg &lt;- cbind(time, step1, ramp1, step2, ramp2, step3, ramp3)\n\ncbind(d2, xreg)\n# month age prevalence time step1 ramp1 step2 ramp2 step3 ramp3\n# 1 2016-01-01 Children 214.1 0 0 0 0 0 0 0\n# 3 2016-02-01 Children 194.9 1 0 0 0 0 0 0\n# 5 2016-03-01 Children 220.3 2 0 0 0 0 0 0\n# 7 2016-04-01 Children 200.7 3 0 0 0 0 0 0\n# 9 2016-05-01 Children 213.5 4 0 0 0 0 0 0\n# 11 2016-06-01 Children 197.6 5 0 0 0 0 0 0\n# 13 2016-07-01 Children 128.2 6 0 0 0 0 0 0\n# 15 2016-08-01 Children 138.6 7 0 0 0 0 0 0\n# 17 2016-09-01 Children 215.0 8 0 0 0 0 0 0\n# 19 2016-10-01 Children 201.9 9 0 0 0 0 0 0\n# 21 2016-11-01 Children 223.8 10 0 0 0 0 0 0\n# 23 2016-12-01 Children 204.9 11 0 0 0 0 0 0\n# 25 2017-01-01 Children 234.1 12 0 0 0 0 0 0\n# 27 2017-02-01 Children 202.0 13 0 0 0 0 0 0\n# 29 2017-03-01 Children 247.0 14 0 0 0 0 0 0\n# 31 2017-04-01 Children 209.8 15 0 0 0 0 0 0\n# 33 2017-05-01 Children 236.7 16 0 0 0 0 0 0\n# 35 2017-06-01 Children 216.9 17 0 0 0 0 0 0\n# 37 2017-07-01 Children 145.1 18 0 0 0 0 0 0\n# 39 2017-08-01 Children 144.3 19 0 0 0 0 0 0\n# 41 2017-09-01 Children 235.1 20 0 0 0 0 0 0\n# 43 2017-10-01 Children 227.5 21 0 0 0 0 0 0\n# 45 2017-11-01 Children 242.5 22 0 0 0 0 0 0\n# 47 2017-12-01 Children 229.5 23 0 0 0 0 0 0\n# 49 2018-01-01 Children 255.3 24 0 0 0 0 0 0\n# 51 2018-02-01 Children 226.3 25 0 0 0 0 0 0\n# 53 2018-03-01 Children 260.8 26 0 0 0 0 0 0\n# 55 2018-04-01 Children 229.7 27 0 0 0 0 0 0\n# 57 2018-05-01 Children 245.5 28 0 0 0 0 0 0\n# 59 2018-06-01 Children 232.6 29 0 0 0 0 0 0\n# 61 2018-07-01 Children 158.8 30 0 0 0 0 0 0\n# 63 2018-08-01 Children 165.0 31 0 0 0 0 0 0\n# 65 2018-09-01 Children 244.3 32 0 0 0 0 0 0\n# 67 2018-10-01 Children 249.4 33 0 0 0 0 0 0\n# 69 2018-11-01 Children 259.4 34 0 0 0 0 0 0\n# 71 2018-12-01 Children 244.0 35 0 0 0 0 0 0\n# 73 2019-01-01 Children 275.7 36 0 0 0 0 0 0\n# 75 2019-02-01 Children 240.7 37 0 0 0 0 0 0\n# 77 2019-03-01 Children 274.4 38 0 0 0 0 0 0\n# 79 2019-04-01 Children 256.8 39 0 0 0 0 0 0\n# 81 2019-05-01 Children 274.3 40 0 0 0 0 0 0\n# 83 2019-06-01 Children 240.3 41 0 0 0 0 0 0\n# 85 2019-07-01 Children 178.2 42 0 0 0 0 0 0\n# 87 2019-08-01 Children 180.3 43 0 0 0 0 0 0\n# 89 2019-09-01 Children 264.0 44 0 0 0 0 0 0\n# 91 2019-10-01 Children 271.3 45 0 0 0 0 0 0\n# 93 2019-11-01 Children 277.8 46 0 0 0 0 0 0\n# 95 2019-12-01 Children 261.5 47 0 0 0 0 0 0\n# 97 2020-01-01 Children 296.9 48 0 0 0 0 0 0\n# 99 2020-02-01 Children 265.8 49 0 0 0 0 0 0\n# 101 2020-03-01 Children 270.4 50 0 0 0 0 0 0\n# 103 2020-04-01 Children 218.8 51 0 0 0 0 0 0\n# 105 2020-05-01 Children 214.7 52 0 0 0 0 0 0\n# 107 2020-06-01 Children 221.8 53 0 0 0 0 0 0\n# 109 2020-07-01 Children 183.6 54 0 0 0 0 0 0\n# 111 2020-08-01 Children 185.4 55 0 0 0 0 0 0\n# 113 2020-09-01 Children 273.6 56 0 0 0 0 0 0\n# 115 2020-10-01 Children 276.7 57 0 0 0 0 0 0\n# 117 2020-11-01 Children 285.0 58 0 0 0 0 0 0\n# 119 2020-12-01 Children 282.8 59 0 0 0 0 0 0\n# 121 2021-01-01 Children 308.3 60 0 0 0 0 0 0\n# 123 2021-02-01 Children 279.2 61 0 0 0 0 0 0\n# 125 2021-03-01 Children 331.1 62 0 0 0 0 0 0\n# 127 2021-04-01 Children 289.3 63 0 0 0 0 0 0\n# 129 2021-05-01 Children 303.0 64 0 0 0 0 0 0\n# 131 2021-06-01 Children 307.5 65 0 0 0 0 0 0\n# 133 2021-07-01 Children 221.2 66 0 0 0 0 0 0\n# 135 2021-08-01 Children 215.4 67 0 0 0 0 0 0\n# 137 2021-09-01 Children 324.0 68 0 0 0 0 0 0\n# 139 2021-10-01 Children 323.3 69 1 0 0 0 0 0\n# 141 2021-11-01 Children 328.3 70 1 1 0 0 0 0\n# 143 2021-12-01 Children 326.5 71 1 2 0 0 0 0\n# 145 2022-01-01 Children 349.2 72 1 3 0 0 0 0\n# 147 2022-02-01 Children 319.1 73 1 4 0 0 0 0\n# 149 2022-03-01 Children 379.3 74 1 5 0 0 0 0\n# 151 2022-04-01 Children 341.7 75 1 6 0 0 0 0\n# 153 2022-05-01 Children 365.1 76 1 7 0 0 0 0\n# 155 2022-06-01 Children 351.7 77 1 8 0 0 0 0\n# 157 2022-07-01 Children 253.7 78 1 9 1 0 0 0\n# 159 2022-08-01 Children 266.9 79 1 10 1 1 0 0\n# 161 2022-09-01 Children 375.0 80 1 11 1 2 0 0\n# 163 2022-10-01 Children 373.5 81 1 12 1 3 0 0\n# 165 2022-11-01 Children 385.8 82 1 13 1 4 0 0\n# 167 2022-12-01 Children 368.1 83 1 14 1 5 0 0\n# 169 2023-01-01 Children 413.7 84 1 15 1 6 0 0\n# 171 2023-02-01 Children 376.0 85 1 16 1 7 0 0\n# 173 2023-03-01 Children 440.1 86 1 17 1 8 0 0\n# 175 2023-04-01 Children 390.3 87 1 18 1 9 0 0\n# 177 2023-05-01 Children 409.5 88 1 19 1 10 0 0\n# 179 2023-06-01 Children 402.0 89 1 20 1 11 0 0\n# 181 2023-07-01 Children 297.9 90 1 21 1 12 1 0\n# 183 2023-08-01 Children 297.1 91 1 22 1 13 1 1\n# 185 2023-09-01 Children 430.9 92 1 23 1 14 1 2\n# 187 2023-10-01 Children 433.4 93 1 24 1 15 1 3\n# 189 2023-11-01 Children 454.1 94 1 25 1 16 1 4\n# 191 2023-12-01 Children 441.4 95 1 26 1 17 1 5\n# 193 2024-01-01 Children 472.9 96 1 27 1 18 1 6\n# 195 2024-02-01 Children 455.8 97 1 28 1 19 1 7\n# 197 2024-03-01 Children 496.3 98 1 29 1 20 1 8\n# 199 2024-04-01 Children 475.0 99 1 30 1 21 1 9\n# 201 2024-05-01 Children 483.4 100 1 31 1 22 1 10\n# 203 2024-06-01 Children 466.0 101 1 32 1 23 1 11\n# 205 2024-07-01 Children 367.3 102 1 33 1 24 1 12\n# 207 2024-08-01 Children 368.4 103 1 34 1 25 1 13\n# 209 2024-09-01 Children 499.4 104 1 35 1 26 1 14\n# 211 2024-10-01 Children 528.3 105 1 36 1 27 1 15\n# 213 2024-11-01 Children 533.8 106 1 37 1 28 1 16\n# 215 2024-12-01 Children 525.6 107 1 38 1 29 1 17\n# 217 2025-01-01 Children 588.3 108 1 39 1 30 1 18\n# 219 2025-02-01 Children 532.4 109 1 40 1 31 1 19\n# 221 2025-03-01 Children 599.6 110 1 41 1 32 1 20\n# 223 2025-04-01 Children 580.3 111 1 42 1 33 1 21\n# 225 2025-05-01 Children 601.5 112 1 43 1 34 1 22\n# 227 2025-06-01 Children 560.6 113 1 44 1 35 1 23\n# 229 2025-07-01 Children 455.2 114 1 45 1 36 1 24\n# 231 2025-08-01 Children 440.6 115 1 46 1 37 1 25\n# 233 2025-09-01 Children 629.2 116 1 47 1 38 1 26\n# 235 2025-10-01 Children 655.7 117 1 48 1 39 1 27\n# 237 2025-11-01 Children 653.8 118 1 49 1 40 1 28\n# 239 2025-12-01 Children 674.9 119 1 50 1 41 1 29\n</code></pre>\n<p>According to <a href=\"https://stats.stackexchange.com/q/665216/\">this thread</a>, I included the ITS variables for the 3 interventions in a single model. I used <code>forecast::auto.arima()</code> to select (p, q) and (P, Q) parameters automatically.</p>\n<p>According to <a href=\"https://doi.org/10.1186/s12874-021-01235-8\" rel=\"nofollow noreferrer\">Schaffer et al.</a>, I included a <code>time</code> variable and set <span class=\"math-container\">$d = 0$</span> and <span class=\"math-container\">$D = 0$</span> (but I am not sure that I need both <span class=\"math-container\">$d = 0$</span> and <span class=\"math-container\">$D = 0$</span>): (emphasis mine)</p>\n<blockquote>\n<p>In ITS analysis, ARIMA forecasts <span class=\"math-container\">$Y_t$</span> in the absence of the intervention (the “counterfactual”) and determines how the observed diverges from this forecast. Unlike segmented regression, including time or seasonal dummy variables in the ARIMA model is not necessary, as ARIMA can eliminate trends and seasonality through differencing. <em>If the trend is eliminated via differencing then the pre- and post-intervention trends cannot be estimated from the model. However, if estimation of the pre- and/or post-intervention slope is desired, this can be accommodated by including time as a covariate and incorporating AR and MA terms to address autocorrelation (e.g. ARMA models)</em> [<a href=\"https://doi.org/10.1186/s12889-017-4998-9\" rel=\"nofollow noreferrer\">21</a>, <a href=\"https://doi.org/10.3111/13696998.2011.626097\" rel=\"nofollow noreferrer\">22</a>].</p>\n</blockquote>\n<pre class=\"lang-r prettyprint-override\"><code>fit &lt;- forecast::auto.arima(\n d2$prevalence,\n d = 0,\n D = 0,\n stepwise = FALSE,\n trace = TRUE,\n approximation = FALSE,\n xreg = xreg\n)\n# \n# Regression with ARIMA(0,0,0) errors : 1422.701\n# Regression with ARIMA(0,0,0) errors : 1256.821\n# Regression with ARIMA(0,0,0)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,0)(0,0,1)[12] errors : 1186.586\n# Regression with ARIMA(0,0,0)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,0)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,2)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,1) errors : 1348.712\n# Regression with ARIMA(0,0,1) errors : 1237.018\n# Regression with ARIMA(0,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,1)(0,0,1)[12] errors : 1167.011\n# Regression with ARIMA(0,0,1)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,1)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,1)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,1)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,2)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,1)(2,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,2) errors : 1304.633\n# Regression with ARIMA(0,0,2) errors : 1235.236\n# Regression with ARIMA(0,0,2)(0,0,1)[12] errors : 1226.35\n# Regression with ARIMA(0,0,2)(0,0,1)[12] errors : 1164.06\n# Regression with ARIMA(0,0,2)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,2)(0,0,2)[12] errors : 1122.153\n# ARIMA(0,0,2)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,2)(1,0,0)[12] errors : Inf\n# ARIMA(0,0,2)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,2)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,2)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,2)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,2)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,3) errors : 1301.979\n# Regression with ARIMA(0,0,3) errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,1)[12] errors : 1166.444\n# Regression with ARIMA(0,0,3)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,3)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,3)(1,0,0)[12] errors : Inf\n# ARIMA(0,0,3)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,3)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,3)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,3)(2,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,4) errors : Inf\n# Regression with ARIMA(0,0,4) errors : Inf\n# Regression with ARIMA(0,0,4)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,4)(0,0,1)[12] errors : 1167.795\n# ARIMA(0,0,4)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,4)(1,0,0)[12] errors : Inf\n# Regression with ARIMA(0,0,5) errors : Inf\n# Regression with ARIMA(0,0,5) errors : Inf\n# Regression with ARIMA(1,0,0) errors : 1269.039\n# Regression with ARIMA(1,0,0) errors : 1236.551\n# Regression with ARIMA(1,0,0)(0,0,1)[12] errors : 1191.463\n# Regression with ARIMA(1,0,0)(0,0,1)[12] errors : 1163.925\n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors : 1144.206\n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors : 1122.139\n# ARIMA(1,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,0)(2,0,0)[12] errors : Inf\n# ARIMA(1,0,0)(2,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,0)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(2,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,0)(2,0,2)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,1) errors : 1269.741\n# Regression with ARIMA(1,0,1) errors : 1237.127\n# Regression with ARIMA(1,0,1)(0,0,1)[12] errors : 1191.287\n# Regression with ARIMA(1,0,1)(0,0,1)[12] errors : 1165.649\n# Regression with ARIMA(1,0,1)(0,0,2)[12] errors : 1145.611\n# Regression with ARIMA(1,0,1)(0,0,2)[12] errors : 1123.56\n# ARIMA(1,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(1,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,1)(2,0,0)[12] errors : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,2) errors : Inf\n# ARIMA(1,0,2) with non-zero mean : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,2) errors : Inf\n# ARIMA(1,0,2) with non-zero mean : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,2)[12] errors : Inf\n# ARIMA(1,0,2)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,2)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,2)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,2)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,2)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,2)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(1,0,3) errors : Inf\n# Regression with ARIMA(1,0,3) errors : Inf\n# Regression with ARIMA(1,0,3)(0,0,1)[12] errors : 1196.785\n# Regression with ARIMA(1,0,3)(0,0,1)[12] errors : Inf\n# ARIMA(1,0,3)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,3)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,4) errors : Inf\n# Regression with ARIMA(1,0,4) errors : Inf\n# Regression with ARIMA(2,0,0) errors : 1270.227\n# Regression with ARIMA(2,0,0) errors : 1235.453\n# Regression with ARIMA(2,0,0)(0,0,1)[12] errors : 1191.855\n# Regression with ARIMA(2,0,0)(0,0,1)[12] errors : 1165.096\n# Regression with ARIMA(2,0,0)(0,0,2)[12] errors : 1145.887\n# Regression with ARIMA(2,0,0)(0,0,2)[12] errors : 1123.031\n# ARIMA(2,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,0)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(2,0,1)[12] with zero mean : Inf\n# Regression with ARIMA(2,0,0)(2,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1) errors : Inf\n# Regression with ARIMA(2,0,1) errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,2)[12] errors : Inf\n# ARIMA(2,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,1)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(2,0,1)(1,0,1)[12] errors : Inf\n# ARIMA(2,0,1)(1,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(2,0,1)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(2,0,2) errors : Inf\n# Regression with ARIMA(2,0,2) errors : Inf\n# ARIMA(2,0,2)(0,0,1)[12] with zero mean : Inf\n# Regression with ARIMA(2,0,2)(0,0,1)[12] errors : Inf\n# ARIMA(2,0,2)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,2)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,3) with zero mean : Inf\n# Regression with ARIMA(2,0,3) errors : Inf\n# Regression with ARIMA(3,0,0) errors : 1271.552\n# Regression with ARIMA(3,0,0) errors : 1234.442\n# Regression with ARIMA(3,0,0)(0,0,1)[12] errors : 1193.944\n# Regression with ARIMA(3,0,0)(0,0,1)[12] errors : 1165.004\n# Regression with ARIMA(3,0,0)(0,0,2)[12] errors : 1147.337\n# Regression with ARIMA(3,0,0)(0,0,2)[12] errors : 1124.69\n# ARIMA(3,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(3,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(3,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(3,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,0)(2,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(3,0,1) errors : Inf\n# Regression with ARIMA(3,0,1) errors : Inf\n# Regression with ARIMA(3,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(3,0,1)(0,0,1)[12] errors : Inf\n# ARIMA(3,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,1)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(3,0,2) errors : Inf\n# Regression with ARIMA(3,0,2) errors : Inf\n# Regression with ARIMA(4,0,0) errors : 1268.176\n# Regression with ARIMA(4,0,0) errors : 1236.501\n# Regression with ARIMA(4,0,0)(0,0,1)[12] errors : 1191.278\n# Regression with ARIMA(4,0,0)(0,0,1)[12] errors : 1167.39\n# ARIMA(4,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(4,0,0)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(4,0,1) errors : Inf\n# Regression with ARIMA(4,0,1) errors : Inf\n# Regression with ARIMA(5,0,0) errors : 1270.388\n# Regression with ARIMA(5,0,0) errors : 1226.449\n# \n# \n# \n# Best model: Regression with ARIMA(1,0,0)(0,0,2)[12] errors \n# \n\nsummary(fit)\n# Series: d2$prevalence \n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors \n# \n# Coefficients:\n# ar1 sma1 sma2 intercept time step1 ramp1 step2 ramp2 step3 ramp3\n# 0.5256 1.0413 0.6987 188.8179 1.5717 3.3284 2.1966 -5.6181 0.4640 0.4378 4.6447\n# s.e. 0.0800 0.1536 0.1312 20.6895 0.4724 13.0492 3.5219 23.9955 3.8197 19.1790 2.5724\n# \n# sigma^2 = 494.9: log likelihood = -547.61\n# AIC=1119.22 AICc=1122.14 BIC=1152.67\n# \n# Training set error measures:\n# ME RMSE MAE MPE MAPE MASE ACF1\n# Training set -0.2760281 21.20232 16.21419 -0.9807653 5.578825 0.3604042 0.05861382\n</code></pre>\n<h2>Question</h2>\n<p>Should I just apply the same strategy to adults? Or can such models handle correlated/stratified time series?</p>\n<p>I guess that there is a better strategy than fitting separate models within each subgroup. However, I suspect that I cannot simply stack the time series and include <code>age</code> in <code>xreg</code>. Additionally, I probably need an interaction terms (e.g. <code>time * age</code>).</p>\n<p>I have also read about <a href=\"https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/gls.html\" rel=\"nofollow noreferrer\"><code>nlme::gls()</code></a> but I would like guidance/confirmation whether it is more appropriate.</p>\n<hr />\n<p>BTW, I am not satisfied with the fit, but I will explore that once I have decided the modelling strategy.</p>\n<pre class=\"lang-r prettyprint-override\"><code>res &lt;- residuals(fit)\nl &lt;- 36\n\npar(mfrow = c(3, 1))\nplot(res)\nacf(res, lag.max = l)\npacf(res, lag.max = l)\ndev.off()\n\nBox.test(res, lag = 24, type = &quot;Ljung-Box&quot;, fitdf = 3)\n# \n# Box-Ljung test\n# \n# data: res\n# X-squared = 49.814, df = 21, p-value = 0.0003872\n# \n</code></pre>\n<p><a href=\"https://i.sstatic.net/pzs11NTf.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/pzs11NTf.png\" alt=\"residuals\" /></a></p>\n", "question_id": 676979, "question_license": "CC BY-SA 4.0", "question_score": 1, "question_text": "Background\n\n\n\n\n\nData: time series of the monthly prevalence of patients receiving a given medication by age group (children or adults) from January 2016 to December 2026\n\n\n\n\nInterventions: three successive regulatory interventions implemented on September 2021, June 2022, and June 2023 to facilitate prescription of the medication (dotted lines on the figure below)\n\n\n\n\nAim: to estimate pre-intervention slope, post-intervention 1 slope, post-intervention 2 slope, and post-intervention 3 slope depending on age group to assess the impact of the interventions (i.e., interrupted time series analysis, ITS)\n\n\n\n\n\nmonth <- seq(\n from = as.Date(\"2016-01-01\"),\n to = as.Date(\"2025-12-31\"),\n by = \"month\"\n)\nmonth <- rep(month, each = 2)\nage <- rep(c(\"Children\", \"Adults\"), 120)\nprevalence <- c(\n 214.1, 11.3, 194.9, 11.1, 220.3, 11.8, 200.7, 11.7, 213.5, \n 11.7, 197.6, 11.5, 128.2, 10.3, 138.6, 9.9, 215, 12.1, 201.9, \n 11.9, 223.8, 12.2, 204.9, 12.4, 234.1, 12.7, 202, 11.9, 247, \n 13.6, 209.8, 13, 236.7, 13.4, 216.9, 13.1, 145.1, 12, 144.3, \n 11, 235.1, 13.7, 227.5, 13.9, 242.5, 13.9, 229.5, 13.8, 255.3, \n 14.5, 226.3, 13.6, 260.8, 15.2, 229.7, 14.2, 245.5, 14.7, 232.6, \n 14.4, 158.8, 13.4, 165, 12.5, 244.3, 14.8, 249.4, 15.7, 259.4, \n 15.5, 244, 15.3, 275.7, 16.2, 240.7, 14.9, 274.4, 16.2, 256.8, \n 16.3, 274.3, 16.4, 240.3, 15.5, 178.2, 15.5, 180.3, 13.6, 264, \n 16.3, 271.3, 17.4, 277.8, 16.9, 261.5, 16.8, 296.9, 17.8, 265.8, \n 17.2, 270.4, 17.9, 218.8, 16, 214.7, 16, 221.8, 16.7, 183.6, \n 16.8, 185.4, 14.9, 273.6, 18.7, 276.7, 19.3, 285, 19.4, 282.8, \n 19.9, 308.3, 20.2, 279.2, 19.3, 331.1, 22, 289.3, 21.2, 303, \n 20.9, 307.5, 21.8, 221.2, 20.9, 215.4, 18.9, 324, 23.4, 323.3, \n 24.3, 328.3, 24.7, 326.5, 25.8, 349.2, 26.1, 319.1, 25.5, 379.3, \n 29, 341.7, 28.3, 365.1, 28.6, 351.7, 30.1, 253.7, 28.6, 266.9, \n 27, 375, 33.4, 373.5, 34.4, 385.8, 35.5, 368.1, 36, 413.7, 38.8, \n 376, 37.4, 440.1, 42.5, 390.3, 40.8, 409.5, 42.6, 402, 44.9, \n 297.9, 43.4, 297.1, 40.2, 430.9, 49.3, 433.4, 51.2, 454.1, 53.1, \n 441.4, 53.4, 472.9, 57, 455.8, 57, 496.3, 62.6, 475, 63.1, 483.4, \n 63.3, 466, 65.1, 367.3, 66.2, 368.4, 58.6, 499.4, 70.8, 528.3, \n 76.1, 533.8, 76, 525.6, 77.2, 588.3, 84.8, 532.4, 81.1, 599.6, \n 90.9, 580.3, 90.8, 601.5, 93.8, 560.6, 95.1, 455.2, 96.9, 440.6, \n 83.7, 629.2, 107.7, 655.7, 112.8, 653.8, 113.2, 674.9, 118.7\n)\nd <- data.frame(month, age, prevalence)\n\npar(mfrow = c(1, 2))\nplot(prevalence ~ month, d, type = \"l\", main = \"Children\", subset = age == \"Children\")\nplot(prevalence ~ month, d, type = \"l\", main = \"Adults\", subset = age == \"Adults\")\ndev.off()\n\n\n\n\n\n[image: time series; source: https://i.sstatic.net/4a0pdydL.png] (https://i.sstatic.net/4a0pdydL.png)\n\n\n\n\nWhat I did\n\n\n\n\nI started with children.\n\n\n\n\nI constructed the ITS variables for the 3 interventions (step1, ramp1, step2, ramp2, step3, ramp3) following Schaffer et al. (https://doi.org/10.1186/s12874-021-01235-8) and Xiao et al. (https://doi.org/10.1093/ije/dyaa148)\n\n\n\n\nd2 <- subset(d, age == \"Children\")\nd2$prevalence <- ts(d2$prevalence, start = 2016, frequency = 12)\n\ninterventions <- as.Date(c(\"2021-09-13\", \"2022-06-02\", \"2023-06-29\"))\n\nstep1 <- as.numeric(d2$month >= interventions[1])\nramp1 <- ifelse(step1 == 1, cumsum(step1) - 1, 0)\n\nstep2 <- as.numeric(d2$month >= interventions[2])\nramp2 <- ifelse(step2 == 1, cumsum(step2) - 1, 0)\n\nstep3 <- as.numeric(d2$month >= interventions[3])\nramp3 <- ifelse(step3 == 1, cumsum(step3) - 1, 0)\n\ntime <- seq(from = 0, along.with = d2$month)\n\nxreg <- cbind(time, step1, ramp1, step2, ramp2, step3, ramp3)\n\ncbind(d2, xreg)\n# month age prevalence time step1 ramp1 step2 ramp2 step3 ramp3\n# 1 2016-01-01 Children 214.1 0 0 0 0 0 0 0\n# 3 2016-02-01 Children 194.9 1 0 0 0 0 0 0\n# 5 2016-03-01 Children 220.3 2 0 0 0 0 0 0\n# 7 2016-04-01 Children 200.7 3 0 0 0 0 0 0\n# 9 2016-05-01 Children 213.5 4 0 0 0 0 0 0\n# 11 2016-06-01 Children 197.6 5 0 0 0 0 0 0\n# 13 2016-07-01 Children 128.2 6 0 0 0 0 0 0\n# 15 2016-08-01 Children 138.6 7 0 0 0 0 0 0\n# 17 2016-09-01 Children 215.0 8 0 0 0 0 0 0\n# 19 2016-10-01 Children 201.9 9 0 0 0 0 0 0\n# 21 2016-11-01 Children 223.8 10 0 0 0 0 0 0\n# 23 2016-12-01 Children 204.9 11 0 0 0 0 0 0\n# 25 2017-01-01 Children 234.1 12 0 0 0 0 0 0\n# 27 2017-02-01 Children 202.0 13 0 0 0 0 0 0\n# 29 2017-03-01 Children 247.0 14 0 0 0 0 0 0\n# 31 2017-04-01 Children 209.8 15 0 0 0 0 0 0\n# 33 2017-05-01 Children 236.7 16 0 0 0 0 0 0\n# 35 2017-06-01 Children 216.9 17 0 0 0 0 0 0\n# 37 2017-07-01 Children 145.1 18 0 0 0 0 0 0\n# 39 2017-08-01 Children 144.3 19 0 0 0 0 0 0\n# 41 2017-09-01 Children 235.1 20 0 0 0 0 0 0\n# 43 2017-10-01 Children 227.5 21 0 0 0 0 0 0\n# 45 2017-11-01 Children 242.5 22 0 0 0 0 0 0\n# 47 2017-12-01 Children 229.5 23 0 0 0 0 0 0\n# 49 2018-01-01 Children 255.3 24 0 0 0 0 0 0\n# 51 2018-02-01 Children 226.3 25 0 0 0 0 0 0\n# 53 2018-03-01 Children 260.8 26 0 0 0 0 0 0\n# 55 2018-04-01 Children 229.7 27 0 0 0 0 0 0\n# 57 2018-05-01 Children 245.5 28 0 0 0 0 0 0\n# 59 2018-06-01 Children 232.6 29 0 0 0 0 0 0\n# 61 2018-07-01 Children 158.8 30 0 0 0 0 0 0\n# 63 2018-08-01 Children 165.0 31 0 0 0 0 0 0\n# 65 2018-09-01 Children 244.3 32 0 0 0 0 0 0\n# 67 2018-10-01 Children 249.4 33 0 0 0 0 0 0\n# 69 2018-11-01 Children 259.4 34 0 0 0 0 0 0\n# 71 2018-12-01 Children 244.0 35 0 0 0 0 0 0\n# 73 2019-01-01 Children 275.7 36 0 0 0 0 0 0\n# 75 2019-02-01 Children 240.7 37 0 0 0 0 0 0\n# 77 2019-03-01 Children 274.4 38 0 0 0 0 0 0\n# 79 2019-04-01 Children 256.8 39 0 0 0 0 0 0\n# 81 2019-05-01 Children 274.3 40 0 0 0 0 0 0\n# 83 2019-06-01 Children 240.3 41 0 0 0 0 0 0\n# 85 2019-07-01 Children 178.2 42 0 0 0 0 0 0\n# 87 2019-08-01 Children 180.3 43 0 0 0 0 0 0\n# 89 2019-09-01 Children 264.0 44 0 0 0 0 0 0\n# 91 2019-10-01 Children 271.3 45 0 0 0 0 0 0\n# 93 2019-11-01 Children 277.8 46 0 0 0 0 0 0\n# 95 2019-12-01 Children 261.5 47 0 0 0 0 0 0\n# 97 2020-01-01 Children 296.9 48 0 0 0 0 0 0\n# 99 2020-02-01 Children 265.8 49 0 0 0 0 0 0\n# 101 2020-03-01 Children 270.4 50 0 0 0 0 0 0\n# 103 2020-04-01 Children 218.8 51 0 0 0 0 0 0\n# 105 2020-05-01 Children 214.7 52 0 0 0 0 0 0\n# 107 2020-06-01 Children 221.8 53 0 0 0 0 0 0\n# 109 2020-07-01 Children 183.6 54 0 0 0 0 0 0\n# 111 2020-08-01 Children 185.4 55 0 0 0 0 0 0\n# 113 2020-09-01 Children 273.6 56 0 0 0 0 0 0\n# 115 2020-10-01 Children 276.7 57 0 0 0 0 0 0\n# 117 2020-11-01 Children 285.0 58 0 0 0 0 0 0\n# 119 2020-12-01 Children 282.8 59 0 0 0 0 0 0\n# 121 2021-01-01 Children 308.3 60 0 0 0 0 0 0\n# 123 2021-02-01 Children 279.2 61 0 0 0 0 0 0\n# 125 2021-03-01 Children 331.1 62 0 0 0 0 0 0\n# 127 2021-04-01 Children 289.3 63 0 0 0 0 0 0\n# 129 2021-05-01 Children 303.0 64 0 0 0 0 0 0\n# 131 2021-06-01 Children 307.5 65 0 0 0 0 0 0\n# 133 2021-07-01 Children 221.2 66 0 0 0 0 0 0\n# 135 2021-08-01 Children 215.4 67 0 0 0 0 0 0\n# 137 2021-09-01 Children 324.0 68 0 0 0 0 0 0\n# 139 2021-10-01 Children 323.3 69 1 0 0 0 0 0\n# 141 2021-11-01 Children 328.3 70 1 1 0 0 0 0\n# 143 2021-12-01 Children 326.5 71 1 2 0 0 0 0\n# 145 2022-01-01 Children 349.2 72 1 3 0 0 0 0\n# 147 2022-02-01 Children 319.1 73 1 4 0 0 0 0\n# 149 2022-03-01 Children 379.3 74 1 5 0 0 0 0\n# 151 2022-04-01 Children 341.7 75 1 6 0 0 0 0\n# 153 2022-05-01 Children 365.1 76 1 7 0 0 0 0\n# 155 2022-06-01 Children 351.7 77 1 8 0 0 0 0\n# 157 2022-07-01 Children 253.7 78 1 9 1 0 0 0\n# 159 2022-08-01 Children 266.9 79 1 10 1 1 0 0\n# 161 2022-09-01 Children 375.0 80 1 11 1 2 0 0\n# 163 2022-10-01 Children 373.5 81 1 12 1 3 0 0\n# 165 2022-11-01 Children 385.8 82 1 13 1 4 0 0\n# 167 2022-12-01 Children 368.1 83 1 14 1 5 0 0\n# 169 2023-01-01 Children 413.7 84 1 15 1 6 0 0\n# 171 2023-02-01 Children 376.0 85 1 16 1 7 0 0\n# 173 2023-03-01 Children 440.1 86 1 17 1 8 0 0\n# 175 2023-04-01 Children 390.3 87 1 18 1 9 0 0\n# 177 2023-05-01 Children 409.5 88 1 19 1 10 0 0\n# 179 2023-06-01 Children 402.0 89 1 20 1 11 0 0\n# 181 2023-07-01 Children 297.9 90 1 21 1 12 1 0\n# 183 2023-08-01 Children 297.1 91 1 22 1 13 1 1\n# 185 2023-09-01 Children 430.9 92 1 23 1 14 1 2\n# 187 2023-10-01 Children 433.4 93 1 24 1 15 1 3\n# 189 2023-11-01 Children 454.1 94 1 25 1 16 1 4\n# 191 2023-12-01 Children 441.4 95 1 26 1 17 1 5\n# 193 2024-01-01 Children 472.9 96 1 27 1 18 1 6\n# 195 2024-02-01 Children 455.8 97 1 28 1 19 1 7\n# 197 2024-03-01 Children 496.3 98 1 29 1 20 1 8\n# 199 2024-04-01 Children 475.0 99 1 30 1 21 1 9\n# 201 2024-05-01 Children 483.4 100 1 31 1 22 1 10\n# 203 2024-06-01 Children 466.0 101 1 32 1 23 1 11\n# 205 2024-07-01 Children 367.3 102 1 33 1 24 1 12\n# 207 2024-08-01 Children 368.4 103 1 34 1 25 1 13\n# 209 2024-09-01 Children 499.4 104 1 35 1 26 1 14\n# 211 2024-10-01 Children 528.3 105 1 36 1 27 1 15\n# 213 2024-11-01 Children 533.8 106 1 37 1 28 1 16\n# 215 2024-12-01 Children 525.6 107 1 38 1 29 1 17\n# 217 2025-01-01 Children 588.3 108 1 39 1 30 1 18\n# 219 2025-02-01 Children 532.4 109 1 40 1 31 1 19\n# 221 2025-03-01 Children 599.6 110 1 41 1 32 1 20\n# 223 2025-04-01 Children 580.3 111 1 42 1 33 1 21\n# 225 2025-05-01 Children 601.5 112 1 43 1 34 1 22\n# 227 2025-06-01 Children 560.6 113 1 44 1 35 1 23\n# 229 2025-07-01 Children 455.2 114 1 45 1 36 1 24\n# 231 2025-08-01 Children 440.6 115 1 46 1 37 1 25\n# 233 2025-09-01 Children 629.2 116 1 47 1 38 1 26\n# 235 2025-10-01 Children 655.7 117 1 48 1 39 1 27\n# 237 2025-11-01 Children 653.8 118 1 49 1 40 1 28\n# 239 2025-12-01 Children 674.9 119 1 50 1 41 1 29\n\n\n\n\n\nAccording to this thread (https://stats.stackexchange.com/q/665216/), I included the ITS variables for the 3 interventions in a single model. I used forecast::auto.arima() to select (p, q) and (P, Q) parameters automatically.\n\n\n\n\nAccording to Schaffer et al. (https://doi.org/10.1186/s12874-021-01235-8), I included a time variable and set $d = 0$ and $D = 0$ (but I am not sure that I need both $d = 0$ and $D = 0$): (emphasis mine)\n\n\n\n\n\n\n\nIn ITS analysis, ARIMA forecasts $Y_t$ in the absence of the intervention (the “counterfactual”) and determines how the observed diverges from this forecast. Unlike segmented regression, including time or seasonal dummy variables in the ARIMA model is not necessary, as ARIMA can eliminate trends and seasonality through differencing. If the trend is eliminated via differencing then the pre- and post-intervention trends cannot be estimated from the model. However, if estimation of the pre- and/or post-intervention slope is desired, this can be accommodated by including time as a covariate and incorporating AR and MA terms to address autocorrelation (e.g. ARMA models) [21 (https://doi.org/10.1186/s12889-017-4998-9), 22 (https://doi.org/10.3111/13696998.2011.626097)].\n\n\n\n\n\n\n\nfit <- forecast::auto.arima(\n d2$prevalence,\n d = 0,\n D = 0,\n stepwise = FALSE,\n trace = TRUE,\n approximation = FALSE,\n xreg = xreg\n)\n# \n# Regression with ARIMA(0,0,0) errors : 1422.701\n# Regression with ARIMA(0,0,0) errors : 1256.821\n# Regression with ARIMA(0,0,0)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,0)(0,0,1)[12] errors : 1186.586\n# Regression with ARIMA(0,0,0)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,0)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,0)(2,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,0)(2,0,2)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,1) errors : 1348.712\n# Regression with ARIMA(0,0,1) errors : 1237.018\n# Regression with ARIMA(0,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,1)(0,0,1)[12] errors : 1167.011\n# Regression with ARIMA(0,0,1)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,1)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,1)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,1)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,1)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,1)(2,0,2)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,1)(2,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,2) errors : 1304.633\n# Regression with ARIMA(0,0,2) errors : 1235.236\n# Regression with ARIMA(0,0,2)(0,0,1)[12] errors : 1226.35\n# Regression with ARIMA(0,0,2)(0,0,1)[12] errors : 1164.06\n# Regression with ARIMA(0,0,2)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,2)(0,0,2)[12] errors : 1122.153\n# ARIMA(0,0,2)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,2)(1,0,0)[12] errors : Inf\n# ARIMA(0,0,2)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,2)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(1,0,2)[12] with zero mean : Inf\n# ARIMA(0,0,2)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,2)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(0,0,2)(2,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,2)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,3) errors : 1301.979\n# Regression with ARIMA(0,0,3) errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,1)[12] errors : 1166.444\n# Regression with ARIMA(0,0,3)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(0,0,3)(0,0,2)[12] errors : Inf\n# ARIMA(0,0,3)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,3)(1,0,0)[12] errors : Inf\n# ARIMA(0,0,3)(1,0,1)[12] with zero mean : Inf\n# ARIMA(0,0,3)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(0,0,3)(2,0,0)[12] with zero mean : Inf\n# ARIMA(0,0,3)(2,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(0,0,4) errors : Inf\n# Regression with ARIMA(0,0,4) errors : Inf\n# Regression with ARIMA(0,0,4)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(0,0,4)(0,0,1)[12] errors : 1167.795\n# ARIMA(0,0,4)(1,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(0,0,4)(1,0,0)[12] errors : Inf\n# Regression with ARIMA(0,0,5) errors : Inf\n# Regression with ARIMA(0,0,5) errors : Inf\n# Regression with ARIMA(1,0,0) errors : 1269.039\n# Regression with ARIMA(1,0,0) errors : 1236.551\n# Regression with ARIMA(1,0,0)(0,0,1)[12] errors : 1191.463\n# Regression with ARIMA(1,0,0)(0,0,1)[12] errors : 1163.925\n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors : 1144.206\n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors : 1122.139\n# ARIMA(1,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,0)(2,0,0)[12] errors : Inf\n# ARIMA(1,0,0)(2,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,0)(2,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,0)(2,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,0)(2,0,2)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,1) errors : 1269.741\n# Regression with ARIMA(1,0,1) errors : 1237.127\n# Regression with ARIMA(1,0,1)(0,0,1)[12] errors : 1191.287\n# Regression with ARIMA(1,0,1)(0,0,1)[12] errors : 1165.649\n# Regression with ARIMA(1,0,1)(0,0,2)[12] errors : 1145.611\n# Regression with ARIMA(1,0,1)(0,0,2)[12] errors : 1123.56\n# ARIMA(1,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(1,0,2)[12] with zero mean : Inf\n# ARIMA(1,0,1)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(1,0,1)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,1)(2,0,0)[12] errors : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,2) errors : Inf\n# ARIMA(1,0,2) with non-zero mean : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# ARIMA(1,0,1)(2,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,2) errors : Inf\n# ARIMA(1,0,2) with non-zero mean : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(1,0,2)(0,0,2)[12] errors : Inf\n# ARIMA(1,0,2)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,2)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(1,0,2)(1,0,1)[12] with zero mean : Inf\n# ARIMA(1,0,2)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(1,0,2)(2,0,0)[12] with zero mean : Inf\n# Regression with ARIMA(1,0,2)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(1,0,3) errors : Inf\n# Regression with ARIMA(1,0,3) errors : Inf\n# Regression with ARIMA(1,0,3)(0,0,1)[12] errors : 1196.785\n# Regression with ARIMA(1,0,3)(0,0,1)[12] errors : Inf\n# ARIMA(1,0,3)(1,0,0)[12] with zero mean : Inf\n# ARIMA(1,0,3)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(1,0,4) errors : Inf\n# Regression with ARIMA(1,0,4) errors : Inf\n# Regression with ARIMA(2,0,0) errors : 1270.227\n# Regression with ARIMA(2,0,0) errors : 1235.453\n# Regression with ARIMA(2,0,0)(0,0,1)[12] errors : 1191.855\n# Regression with ARIMA(2,0,0)(0,0,1)[12] errors : 1165.096\n# Regression with ARIMA(2,0,0)(0,0,2)[12] errors : 1145.887\n# Regression with ARIMA(2,0,0)(0,0,2)[12] errors : 1123.031\n# ARIMA(2,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(1,0,2)[12] with zero mean : Inf\n# ARIMA(2,0,0)(1,0,2)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,0)(2,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,0)(2,0,1)[12] with zero mean : Inf\n# Regression with ARIMA(2,0,0)(2,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1) errors : Inf\n# Regression with ARIMA(2,0,1) errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,2)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(0,0,2)[12] errors : Inf\n# ARIMA(2,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,1)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(2,0,1)(1,0,1)[12] errors : Inf\n# ARIMA(2,0,1)(1,0,1)[12] with non-zero mean : Inf\n# Regression with ARIMA(2,0,1)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(2,0,1)(2,0,0)[12] errors : Inf\n# Regression with ARIMA(2,0,2) errors : Inf\n# Regression with ARIMA(2,0,2) errors : Inf\n# ARIMA(2,0,2)(0,0,1)[12] with zero mean : Inf\n# Regression with ARIMA(2,0,2)(0,0,1)[12] errors : Inf\n# ARIMA(2,0,2)(1,0,0)[12] with zero mean : Inf\n# ARIMA(2,0,2)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(2,0,3) with zero mean : Inf\n# Regression with ARIMA(2,0,3) errors : Inf\n# Regression with ARIMA(3,0,0) errors : 1271.552\n# Regression with ARIMA(3,0,0) errors : 1234.442\n# Regression with ARIMA(3,0,0)(0,0,1)[12] errors : 1193.944\n# Regression with ARIMA(3,0,0)(0,0,1)[12] errors : 1165.004\n# Regression with ARIMA(3,0,0)(0,0,2)[12] errors : 1147.337\n# Regression with ARIMA(3,0,0)(0,0,2)[12] errors : 1124.69\n# ARIMA(3,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,0)(1,0,0)[12] with non-zero mean : Inf\n# ARIMA(3,0,0)(1,0,1)[12] with zero mean : Inf\n# ARIMA(3,0,0)(1,0,1)[12] with non-zero mean : Inf\n# ARIMA(3,0,0)(2,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,0)(2,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(3,0,1) errors : Inf\n# Regression with ARIMA(3,0,1) errors : Inf\n# Regression with ARIMA(3,0,1)(0,0,1)[12] errors : Inf\n# Regression with ARIMA(3,0,1)(0,0,1)[12] errors : Inf\n# ARIMA(3,0,1)(1,0,0)[12] with zero mean : Inf\n# ARIMA(3,0,1)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(3,0,2) errors : Inf\n# Regression with ARIMA(3,0,2) errors : Inf\n# Regression with ARIMA(4,0,0) errors : 1268.176\n# Regression with ARIMA(4,0,0) errors : 1236.501\n# Regression with ARIMA(4,0,0)(0,0,1)[12] errors : 1191.278\n# Regression with ARIMA(4,0,0)(0,0,1)[12] errors : 1167.39\n# ARIMA(4,0,0)(1,0,0)[12] with zero mean : Inf\n# ARIMA(4,0,0)(1,0,0)[12] with non-zero mean : Inf\n# Regression with ARIMA(4,0,1) errors : Inf\n# Regression with ARIMA(4,0,1) errors : Inf\n# Regression with ARIMA(5,0,0) errors : 1270.388\n# Regression with ARIMA(5,0,0) errors : 1226.449\n# \n# \n# \n# Best model: Regression with ARIMA(1,0,0)(0,0,2)[12] errors \n# \n\nsummary(fit)\n# Series: d2$prevalence \n# Regression with ARIMA(1,0,0)(0,0,2)[12] errors \n# \n# Coefficients:\n# ar1 sma1 sma2 intercept time step1 ramp1 step2 ramp2 step3 ramp3\n# 0.5256 1.0413 0.6987 188.8179 1.5717 3.3284 2.1966 -5.6181 0.4640 0.4378 4.6447\n# s.e. 0.0800 0.1536 0.1312 20.6895 0.4724 13.0492 3.5219 23.9955 3.8197 19.1790 2.5724\n# \n# sigma^2 = 494.9: log likelihood = -547.61\n# AIC=1119.22 AICc=1122.14 BIC=1152.67\n# \n# Training set error measures:\n# ME RMSE MAE MPE MAPE MASE ACF1\n# Training set -0.2760281 21.20232 16.21419 -0.9807653 5.578825 0.3604042 0.05861382\n\n\n\n\n\nQuestion\n\n\n\n\nShould I just apply the same strategy to adults? Or can such models handle correlated/stratified time series?\n\n\n\n\nI guess that there is a better strategy than fitting separate models within each subgroup. However, I suspect that I cannot simply stack the time series and include age in xreg. Additionally, I probably need an interaction terms (e.g. time * age).\n\n\n\n\nI have also read about nlme::gls() (https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/gls.html) but I would like guidance/confirmation whether it is more appropriate.\n\n\n\n\n\n\n\nBTW, I am not satisfied with the fit, but I will explore that once I have decided the modelling strategy.\n\n\n\n\nres <- residuals(fit)\nl <- 36\n\npar(mfrow = c(3, 1))\nplot(res)\nacf(res, lag.max = l)\npacf(res, lag.max = l)\ndev.off()\n\nBox.test(res, lag = 24, type = \"Ljung-Box\", fitdf = 3)\n# \n# Box-Ljung test\n# \n# data: res\n# X-squared = 49.814, df = 21, p-value = 0.0003872\n# \n\n\n\n\n\n[image: residuals; source: https://i.sstatic.net/pzs11NTf.png] (https://i.sstatic.net/pzs11NTf.png)", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas", "profile_url": "https://stats.stackexchange.com/users/263046/thomas", "user_type": "registered"}, "created_at": "2026-08-27T20:48:18+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "4F68B416-BBB1-4FBC-8F34-014B8106BA46", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/4F68B416-BBB1-4FBC-8F34-014B8106BA46/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas", "profile_url": "https://stats.stackexchange.com/users/263046/thomas", "user_type": "registered"}, "created_at": "2026-08-28T00:03:27+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "FB0908B9-A046-40C1-A08B-E6284215F76C", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/FB0908B9-A046-40C1-A08B-E6284215F76C/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas", "profile_url": "https://stats.stackexchange.com/users/263046/thomas", "user_type": "registered"}, "created_at": "2026-08-28T00:11:08+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "9E9FBC78-84A9-484D-B830-4D1751368045", "revision_number": 3, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/9E9FBC78-84A9-484D-B830-4D1751368045/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas", "profile_url": "https://stats.stackexchange.com/users/263046/thomas", "user_type": "registered"}, "created_at": "2026-08-28T09:19:31+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "F52E35E2-A4F3-4D9C-BBE7-6F9246725A84", "revision_number": 4, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/F52E35E2-A4F3-4D9C-BBE7-6F9246725A84/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/676979/strategy-for-interrupted-time-series-of-correlated-stratified-time-series", "split": "validation", "split_group": "4776dab9c10254abf857639b7a8deff481b81182a68f5330c41a896a7463144f", "tags": ["r", "time-series", "arima", "generalized-least-squares", "intervention-analysis"], "thread_id": "stats:676979", "title": "Strategy for interrupted time series of correlated/stratified time series"}}
{"accepted_answer": null, "other_answers": [], "product": "statistics_qa", "question": "Not sure if someone can help me, but I am trying to fit a model with one fixed continuous predictor (th) to my response variable (Score, continuous proportion without 0 or 1) measured in four different conditions (bd) in the same observers (obs). The data are from a psychological experiment, so I am having a hard time in finding a precedent. The results of the GLMM look too good to be true, so I think something might be very wrong. How can I make sure if the results are okay? Can I have a R^2 over 0.9 for the fixed dispersion model?\n\n\n\n\nI have previously fitted beta regression models separately to each condition, without accounting for repeated measures and random effects, using betareg, and pseudo-R-squared values were also high (ranging from 0.79 to 0.90). Although they are not the same, can they indicate something about this result? And is there a way for me to confirm the appropriateness of this GLMM and its output for my data? Observers' performance is expected to follow this trend, as they can be separated into 2 separate groups by the predictor's test result (a discrimination threshold), but still I have doubts. Maybe the experimental hypothesis is inappropriate, as one group is expected to outperform the other in the threshold, and the response is a hit score based on the control's group performance.\n\n\n\n\nhere's some of the output of the final model, with fixed dispersion:\n\n\n\n\nmod <- glmmTMB(Score ~ th * bd + (1 | obs),\n family = beta_family(link = \"logit\"), data = data_long)\n\n\n AIC BIC logLik -2*log(L) df.resid \n -565.2 -531.6 292.6 -585.2 202 \n\n**Random effects:**\n\nConditional model:\n Groups Name Variance Std.Dev.\n obs (Intercept) 0.03939 0.1985 \nNumber of obs: 212, groups: obs, 53\n\n> model_performance(\n+ mod,\n+ metrics = c(\"R2\", \"AIC\", \"BIC\", \"RMSE\")\n+ )\n# Indices of model performance\n\nAIC | BIC | R2 (cond.) | R2 (marg.) | RMSE\n-------------------------------------------------\n-565.2 | -531.6 | 0.985 | 0.912 | 0.048\n\n\n--- DHARMa diagnostic tests (unconditional simulations, old default) ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.060925, p-value = 0.4108\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.0707, p-value = 0.51\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res\noutliers at both margin(s) = 3, observations = 212, p-value = 0.009158\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.002927826 0.040795871\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.01415094 \n\n\n\n# Intraclass Correlation Coefficient\n\n Adjusted ICC: 0.832\n Unadjusted ICC: 0.073\n\n\n\n\n\n[image: enter image description here; source: https://i.sstatic.net/V0JjVImt.png] (https://i.sstatic.net/V0JjVImt.png)\n[image: enter image description here; source: https://i.sstatic.net/vTBK3rAo.png] (https://i.sstatic.net/vTBK3rAo.png)\n[image: enter image description here; source: https://i.sstatic.net/zOi948b5.png] (https://i.sstatic.net/zOi948b5.png)\n[image: enter image description here; source: https://i.sstatic.net/7o8KKCNe.png] (https://i.sstatic.net/7o8KKCNe.png)\n[image: enter image description here; source: https://i.sstatic.net/Cf66Rzrk.png] (https://i.sstatic.net/Cf66Rzrk.png)\n[image: enter image description here; source: https://i.sstatic.net/JQ7R4P2C.png] (https://i.sstatic.net/JQ7R4P2C.png)\n[image: enter image description here; source: https://i.sstatic.net/ykzaI4V0.png] (https://i.sstatic.net/ykzaI4V0.png)\n[image: enter image description here; source: https://i.sstatic.net/JprOGOJ2.png] (https://i.sstatic.net/JprOGOJ2.png)\n[image: enter image description here; source: https://i.sstatic.net/YYELEFx7.png] (https://i.sstatic.net/YYELEFx7.png)\n\n\n\n\nafter updating the software, with the new dharma package version, that uses conditional simulations as default, instead of unconditional, I now have significant distribution KS tests... How does this affect model validation?\n\n\n\n\n--- DHARMa diagnostic tests (new default: conditional simulations) ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.107, p-value = 0.01559\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.1412, p-value = 0.138\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res\noutliers at both margin(s) = 2, observations = 212, p-value = 0.06782\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.001144546 0.033661358\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.009433962\n\n\n\n\n\n[image: enter image description here; source: https://i.sstatic.net/2fs8yraM.png] (https://i.sstatic.net/2fs8yraM.png)\n[image: enter image description here; source: https://i.sstatic.net/2frGxmzM.png] (https://i.sstatic.net/2frGxmzM.png)\n[image: enter image description here; source: https://i.sstatic.net/iOSbUmj8.png] (https://i.sstatic.net/iOSbUmj8.png)\n[image: enter image description here; source: https://i.sstatic.net/03FfH1CY.png] (https://i.sstatic.net/03FfH1CY.png)\n[image: enter image description here; source: https://i.sstatic.net/2f1IsCZM.png] (https://i.sstatic.net/2f1IsCZM.png)\n\n\n\n\nUpdate: Due to the new conditional DHARMa results, I've compared a model with variable dispersion, which seems to be more adequate. I guess this solves the issue of the R-squared values, as they are now inappropriate to calculate... :)\n\n\n\n\n--- Model comparison (LRT and AIC) ---\nData: data_long\nModels:\nmod_disp_constant: Score ~ th * bd + (1 | obs), zi=~0, disp=~1\nmod_disp_background: Score ~ th * bd + (1 | obs), zi=~0,disp=~background\n Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq) \nmod_disp_constant 10 -565.16 -531.60 292.58 -585.16 \nmod_disp_background 13 -576.10 -532.47 301.05 -602.10 16.937 3 0.0007281 ***\n---\nSignif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1**\n df AIC\nmod_disp_constant 10 -565.1642\nmod_disp_background 13 -576.1012\n\n--- DHARMa diagnostics: varying dispersion, conditional simulation ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.071019, p-value = 0.2353\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.1215, p-value = 0.192\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res_dispbg_conditional\noutliers at both margin(s) = 1, observations = 212, p-value = 0.3456\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.0001194165 0.0259997103\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.004716981 \n\n\n\n\n\n[image: enter image description here; source: https://i.sstatic.net/fmBeMY6t.png] (https://i.sstatic.net/fmBeMY6t.png)\n\n\n\n\n(can't add more plots, I guess)", "record_id": "Scientific-Statistics-QA:stats:676982", "selected_answer": {"answer_html": "<p>Based on what I see in the interaction plot, there seems to be a strong association between your continuous predictor and the outcome. Consequently, the confidence intervals are narrow (so it's not very noisy). This is likely what primarily contributes to the <span class=\"math-container\">$R^2$</span> being high.</p>\n<p>It seems there aren't very large differences between groups wrt the outcome and the interaction isn't very strong. Grayscale is the only standout group. It has the same association but has a lower conditional mean and higher variability. So the group-based effects in the model are driving less of the <span class=\"math-container\">$R^2$</span> than the continuous predictor is.</p>\n<p>The model fitness seems fine too, so its not an obvious mis-specification. There is some by-quantile curvature at times and the QQ plot isn't exactly perfect but they're passable for a model. The results would be scarier if the plots were obviously bad.</p>\n<p>It seems one version of your model has an additional categorical variable (based on the DHARMa plots) which isn't included in your R formula. This predictor could also be contributing to the model <span class=\"math-container\">$R^2$</span> in some way too, but its not clear how.</p>\n", "answer_id": 676994, "answer_text": "Based on what I see in the interaction plot, there seems to be a strong association between your continuous predictor and the outcome. Consequently, the confidence intervals are narrow (so it's not very noisy). This is likely what primarily contributes to the $R^2$ being high.\n\n\n\n\nIt seems there aren't very large differences between groups wrt the outcome and the interaction isn't very strong. Grayscale is the only standout group. It has the same association but has a lower conditional mean and higher variability. So the group-based effects in the model are driving less of the $R^2$ than the continuous predictor is.\n\n\n\n\nThe model fitness seems fine too, so its not an obvious mis-specification. There is some by-quantile curvature at times and the QQ plot isn't exactly perfect but they're passable for a model. The results would be scarier if the plots were obviously bad.\n\n\n\n\nIt seems one version of your model has an additional categorical variable (based on the DHARMa plots) which isn't included in your R formula. This predictor could also be contributing to the model $R^2$ in some way too, but its not clear how.", "answer_url": "https://stats.stackexchange.com/a/676994", "author": "Shawn Hemelstrand", "author_url": "https://stats.stackexchange.com/users/345611/shawn-hemelstrand", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-28T23:57:01+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676982, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Shawn Hemelstrand", "profile_url": "https://stats.stackexchange.com/users/345611/shawn-hemelstrand", "user_type": "registered"}, "created_at": "2026-08-28T23:57:01+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "DB57E1BB-14A5-4645-A356-129C789D7022", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/DB57E1BB-14A5-4645-A356-129C789D7022/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Shawn Hemelstrand", "profile_url": "https://stats.stackexchange.com/users/345611/shawn-hemelstrand", "user_type": "registered"}, "created_at": "2026-08-29T00:41:24+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "0159009C-9F9A-4524-9B80-EAD167E5975F", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/0159009C-9F9A-4524-9B80-EAD167E5975F/view-source"}], "score": 6, "updated_at": "2026-08-29T00:41:24+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": null, "answers": [{"answer_html": "<p>Based on what I see in the interaction plot, there seems to be a strong association between your continuous predictor and the outcome. Consequently, the confidence intervals are narrow (so it's not very noisy). This is likely what primarily contributes to the <span class=\"math-container\">$R^2$</span> being high.</p>\n<p>It seems there aren't very large differences between groups wrt the outcome and the interaction isn't very strong. Grayscale is the only standout group. It has the same association but has a lower conditional mean and higher variability. So the group-based effects in the model are driving less of the <span class=\"math-container\">$R^2$</span> than the continuous predictor is.</p>\n<p>The model fitness seems fine too, so its not an obvious mis-specification. There is some by-quantile curvature at times and the QQ plot isn't exactly perfect but they're passable for a model. The results would be scarier if the plots were obviously bad.</p>\n<p>It seems one version of your model has an additional categorical variable (based on the DHARMa plots) which isn't included in your R formula. This predictor could also be contributing to the model <span class=\"math-container\">$R^2$</span> in some way too, but its not clear how.</p>\n", "answer_id": 676994, "answer_text": "Based on what I see in the interaction plot, there seems to be a strong association between your continuous predictor and the outcome. Consequently, the confidence intervals are narrow (so it's not very noisy). This is likely what primarily contributes to the $R^2$ being high.\n\n\n\n\nIt seems there aren't very large differences between groups wrt the outcome and the interaction isn't very strong. Grayscale is the only standout group. It has the same association but has a lower conditional mean and higher variability. So the group-based effects in the model are driving less of the $R^2$ than the continuous predictor is.\n\n\n\n\nThe model fitness seems fine too, so its not an obvious mis-specification. There is some by-quantile curvature at times and the QQ plot isn't exactly perfect but they're passable for a model. The results would be scarier if the plots were obviously bad.\n\n\n\n\nIt seems one version of your model has an additional categorical variable (based on the DHARMa plots) which isn't included in your R formula. This predictor could also be contributing to the model $R^2$ in some way too, but its not clear how.", "answer_url": "https://stats.stackexchange.com/a/676994", "author": "Shawn Hemelstrand", "author_url": "https://stats.stackexchange.com/users/345611/shawn-hemelstrand", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-08-28T23:57:01+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 676982, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Shawn Hemelstrand", "profile_url": "https://stats.stackexchange.com/users/345611/shawn-hemelstrand", "user_type": "registered"}, "created_at": "2026-08-28T23:57:01+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "DB57E1BB-14A5-4645-A356-129C789D7022", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/DB57E1BB-14A5-4645-A356-129C789D7022/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Shawn Hemelstrand", "profile_url": "https://stats.stackexchange.com/users/345611/shawn-hemelstrand", "user_type": "registered"}, "created_at": "2026-08-29T00:41:24+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "0159009C-9F9A-4524-9B80-EAD167E5975F", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/0159009C-9F9A-4524-9B80-EAD167E5975F/view-source"}], "score": 6, "updated_at": "2026-08-29T00:41:24+00:00"}], "domain": "statistics", "external_links": ["https://i.sstatic.net/03FfH1CY.png", "https://i.sstatic.net/2f1IsCZM.png", "https://i.sstatic.net/2frGxmzM.png", "https://i.sstatic.net/2fs8yraM.png", "https://i.sstatic.net/7o8KKCNe.png", "https://i.sstatic.net/Cf66Rzrk.png", "https://i.sstatic.net/JQ7R4P2C.png", "https://i.sstatic.net/JprOGOJ2.png", "https://i.sstatic.net/V0JjVImt.png", "https://i.sstatic.net/YYELEFx7.png", "https://i.sstatic.net/fmBeMY6t.png", "https://i.sstatic.net/iOSbUmj8.png", "https://i.sstatic.net/vTBK3rAo.png", "https://i.sstatic.net/ykzaI4V0.png", "https://i.sstatic.net/zOi948b5.png"], "medical_sensitive": true, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "Dodo", "question_author_url": "https://stats.stackexchange.com/users/516003/dodo", "question_author_user_type": "registered", "question_created_at": "2026-08-27T23:58:06+00:00", "question_html": "<p>Not sure if someone can help me, but I am trying to fit a model with one fixed continuous predictor (th) to my response variable (Score, continuous proportion without 0 or 1) measured in four different conditions (bd) in the same observers (obs). The data are from a psychological experiment, so I am having a hard time in finding a precedent. The results of the GLMM look too good to be true, so I think something might be very wrong. How can I make sure if the results are okay? Can I have a R^2 over 0.9 for the fixed dispersion model?</p>\n<p>I have previously fitted beta regression models separately to each condition, without accounting for repeated measures and random effects, using <code>betareg</code>, and pseudo-R-squared values were also high (ranging from 0.79 to 0.90). Although they are not the same, can they indicate something about this result? And is there a way for me to confirm the appropriateness of this GLMM and its output for my data? Observers' performance is expected to follow this trend, as they can be separated into 2 separate groups by the predictor's test result (a discrimination threshold), but still I have doubts. Maybe the experimental hypothesis is inappropriate, as one group is expected to outperform the other in the threshold, and the response is a hit score based on the control's group performance.</p>\n<p>here's some of the output of the final model, with fixed dispersion:</p>\n<pre class=\"lang-r prettyprint-override\"><code>mod &lt;- glmmTMB(Score ~ th * bd + (1 | obs),\n family = beta_family(link = &quot;logit&quot;), data = data_long)\n\n\n AIC BIC logLik -2*log(L) df.resid \n -565.2 -531.6 292.6 -585.2 202 \n\n**Random effects:**\n\nConditional model:\n Groups Name Variance Std.Dev.\n obs (Intercept) 0.03939 0.1985 \nNumber of obs: 212, groups: obs, 53\n\n&gt; model_performance(\n+ mod,\n+ metrics = c(&quot;R2&quot;, &quot;AIC&quot;, &quot;BIC&quot;, &quot;RMSE&quot;)\n+ )\n# Indices of model performance\n\nAIC | BIC | R2 (cond.) | R2 (marg.) | RMSE\n-------------------------------------------------\n-565.2 | -531.6 | 0.985 | 0.912 | 0.048\n\n\n--- DHARMa diagnostic tests (unconditional simulations, old default) ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.060925, p-value = 0.4108\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.0707, p-value = 0.51\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res\noutliers at both margin(s) = 3, observations = 212, p-value = 0.009158\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.002927826 0.040795871\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.01415094 \n\n\n\n# Intraclass Correlation Coefficient\n\n Adjusted ICC: 0.832\n Unadjusted ICC: 0.073\n</code></pre>\n<p><a href=\"https://i.sstatic.net/V0JjVImt.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/V0JjVImt.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/vTBK3rAo.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/vTBK3rAo.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/zOi948b5.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/zOi948b5.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/7o8KKCNe.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/7o8KKCNe.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/Cf66Rzrk.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/Cf66Rzrk.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/JQ7R4P2C.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/JQ7R4P2C.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/ykzaI4V0.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/ykzaI4V0.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/JprOGOJ2.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/JprOGOJ2.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/YYELEFx7.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/YYELEFx7.png\" alt=\"enter image description here\" /></a></p>\n<p>after updating the software, with the new dharma package version, that uses conditional simulations as default, instead of unconditional, I now have significant distribution KS tests... How does this affect model validation?</p>\n<pre class=\"lang-r prettyprint-override\"><code>--- DHARMa diagnostic tests (new default: conditional simulations) ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.107, p-value = 0.01559\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.1412, p-value = 0.138\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res\noutliers at both margin(s) = 2, observations = 212, p-value = 0.06782\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.001144546 0.033661358\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.009433962\n</code></pre>\n<p><a href=\"https://i.sstatic.net/2fs8yraM.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/2fs8yraM.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/2frGxmzM.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/2frGxmzM.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/iOSbUmj8.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/iOSbUmj8.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/03FfH1CY.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/03FfH1CY.png\" alt=\"enter image description here\" /></a>\n<a href=\"https://i.sstatic.net/2f1IsCZM.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/2f1IsCZM.png\" alt=\"enter image description here\" /></a></p>\n<p><strong>Update:</strong> Due to the new conditional DHARMa results, I've compared a model with variable dispersion, which seems to be more adequate. I guess this solves the issue of the R-squared values, as they are now inappropriate to calculate... :)</p>\n<pre class=\"lang-r prettyprint-override\"><code>--- Model comparison (LRT and AIC) ---\nData: data_long\nModels:\nmod_disp_constant: Score ~ th * bd + (1 | obs), zi=~0, disp=~1\nmod_disp_background: Score ~ th * bd + (1 | obs), zi=~0,disp=~background\n Df AIC BIC logLik deviance Chisq Chi Df Pr(&gt;Chisq) \nmod_disp_constant 10 -565.16 -531.60 292.58 -585.16 \nmod_disp_background 13 -576.10 -532.47 301.05 -602.10 16.937 3 0.0007281 ***\n---\nSignif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1**\n df AIC\nmod_disp_constant 10 -565.1642\nmod_disp_background 13 -576.1012\n\n--- DHARMa diagnostics: varying dispersion, conditional simulation ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.071019, p-value = 0.2353\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.1215, p-value = 0.192\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res_dispbg_conditional\noutliers at both margin(s) = 1, observations = 212, p-value = 0.3456\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.0001194165 0.0259997103\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.004716981 \n</code></pre>\n<p><a href=\"https://i.sstatic.net/fmBeMY6t.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/fmBeMY6t.png\" alt=\"enter image description here\" /></a></p>\n<p>(can't add more plots, I guess)</p>\n", "question_id": 676982, "question_license": "CC BY-SA 4.0", "question_score": 5, "question_text": "Not sure if someone can help me, but I am trying to fit a model with one fixed continuous predictor (th) to my response variable (Score, continuous proportion without 0 or 1) measured in four different conditions (bd) in the same observers (obs). The data are from a psychological experiment, so I am having a hard time in finding a precedent. The results of the GLMM look too good to be true, so I think something might be very wrong. How can I make sure if the results are okay? Can I have a R^2 over 0.9 for the fixed dispersion model?\n\n\n\n\nI have previously fitted beta regression models separately to each condition, without accounting for repeated measures and random effects, using betareg, and pseudo-R-squared values were also high (ranging from 0.79 to 0.90). Although they are not the same, can they indicate something about this result? And is there a way for me to confirm the appropriateness of this GLMM and its output for my data? Observers' performance is expected to follow this trend, as they can be separated into 2 separate groups by the predictor's test result (a discrimination threshold), but still I have doubts. Maybe the experimental hypothesis is inappropriate, as one group is expected to outperform the other in the threshold, and the response is a hit score based on the control's group performance.\n\n\n\n\nhere's some of the output of the final model, with fixed dispersion:\n\n\n\n\nmod <- glmmTMB(Score ~ th * bd + (1 | obs),\n family = beta_family(link = \"logit\"), data = data_long)\n\n\n AIC BIC logLik -2*log(L) df.resid \n -565.2 -531.6 292.6 -585.2 202 \n\n**Random effects:**\n\nConditional model:\n Groups Name Variance Std.Dev.\n obs (Intercept) 0.03939 0.1985 \nNumber of obs: 212, groups: obs, 53\n\n> model_performance(\n+ mod,\n+ metrics = c(\"R2\", \"AIC\", \"BIC\", \"RMSE\")\n+ )\n# Indices of model performance\n\nAIC | BIC | R2 (cond.) | R2 (marg.) | RMSE\n-------------------------------------------------\n-565.2 | -531.6 | 0.985 | 0.912 | 0.048\n\n\n--- DHARMa diagnostic tests (unconditional simulations, old default) ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.060925, p-value = 0.4108\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.0707, p-value = 0.51\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res\noutliers at both margin(s) = 3, observations = 212, p-value = 0.009158\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.002927826 0.040795871\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.01415094 \n\n\n\n# Intraclass Correlation Coefficient\n\n Adjusted ICC: 0.832\n Unadjusted ICC: 0.073\n\n\n\n\n\n[image: enter image description here; source: https://i.sstatic.net/V0JjVImt.png] (https://i.sstatic.net/V0JjVImt.png)\n[image: enter image description here; source: https://i.sstatic.net/vTBK3rAo.png] (https://i.sstatic.net/vTBK3rAo.png)\n[image: enter image description here; source: https://i.sstatic.net/zOi948b5.png] (https://i.sstatic.net/zOi948b5.png)\n[image: enter image description here; source: https://i.sstatic.net/7o8KKCNe.png] (https://i.sstatic.net/7o8KKCNe.png)\n[image: enter image description here; source: https://i.sstatic.net/Cf66Rzrk.png] (https://i.sstatic.net/Cf66Rzrk.png)\n[image: enter image description here; source: https://i.sstatic.net/JQ7R4P2C.png] (https://i.sstatic.net/JQ7R4P2C.png)\n[image: enter image description here; source: https://i.sstatic.net/ykzaI4V0.png] (https://i.sstatic.net/ykzaI4V0.png)\n[image: enter image description here; source: https://i.sstatic.net/JprOGOJ2.png] (https://i.sstatic.net/JprOGOJ2.png)\n[image: enter image description here; source: https://i.sstatic.net/YYELEFx7.png] (https://i.sstatic.net/YYELEFx7.png)\n\n\n\n\nafter updating the software, with the new dharma package version, that uses conditional simulations as default, instead of unconditional, I now have significant distribution KS tests... How does this affect model validation?\n\n\n\n\n--- DHARMa diagnostic tests (new default: conditional simulations) ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.107, p-value = 0.01559\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.1412, p-value = 0.138\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res\noutliers at both margin(s) = 2, observations = 212, p-value = 0.06782\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.001144546 0.033661358\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.009433962\n\n\n\n\n\n[image: enter image description here; source: https://i.sstatic.net/2fs8yraM.png] (https://i.sstatic.net/2fs8yraM.png)\n[image: enter image description here; source: https://i.sstatic.net/2frGxmzM.png] (https://i.sstatic.net/2frGxmzM.png)\n[image: enter image description here; source: https://i.sstatic.net/iOSbUmj8.png] (https://i.sstatic.net/iOSbUmj8.png)\n[image: enter image description here; source: https://i.sstatic.net/03FfH1CY.png] (https://i.sstatic.net/03FfH1CY.png)\n[image: enter image description here; source: https://i.sstatic.net/2f1IsCZM.png] (https://i.sstatic.net/2f1IsCZM.png)\n\n\n\n\nUpdate: Due to the new conditional DHARMa results, I've compared a model with variable dispersion, which seems to be more adequate. I guess this solves the issue of the R-squared values, as they are now inappropriate to calculate... :)\n\n\n\n\n--- Model comparison (LRT and AIC) ---\nData: data_long\nModels:\nmod_disp_constant: Score ~ th * bd + (1 | obs), zi=~0, disp=~1\nmod_disp_background: Score ~ th * bd + (1 | obs), zi=~0,disp=~background\n Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq) \nmod_disp_constant 10 -565.16 -531.60 292.58 -585.16 \nmod_disp_background 13 -576.10 -532.47 301.05 -602.10 16.937 3 0.0007281 ***\n---\nSignif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1**\n df AIC\nmod_disp_constant 10 -565.1642\nmod_disp_background 13 -576.1012\n\n--- DHARMa diagnostics: varying dispersion, conditional simulation ---\n\n Asymptotic one-sample Kolmogorov-Smirnov test\n\ndata: simulationOutput$scaledResiduals\nD = 0.071019, p-value = 0.2353\nalternative hypothesis: two-sided\n\n\n DHARMa nonparametric dispersion test via sd of residuals fitted vs. simulated\n\ndata: simulationOutput\ndispersion = 1.1215, p-value = 0.192\nalternative hypothesis: two.sided\n\n\n DHARMa outlier test based on exact binomial test with approximate expectations\n\ndata: sim_res_dispbg_conditional\noutliers at both margin(s) = 1, observations = 212, p-value = 0.3456\nalternative hypothesis: true probability of success is not equal to 0.001998002\n95 percent confidence interval:\n 0.0001194165 0.0259997103\nsample estimates:\nfrequency of outliers (expected: 0.001998001998002 ) \n 0.004716981 \n\n\n\n\n\n[image: enter image description here; source: https://i.sstatic.net/fmBeMY6t.png] (https://i.sstatic.net/fmBeMY6t.png)\n\n\n\n\n(can't add more plots, I guess)", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Dodo", "profile_url": "https://stats.stackexchange.com/users/516003/dodo", "user_type": "registered"}, "created_at": "2026-08-27T23:58:06+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "AD9037CD-0DDE-403F-9F6A-53908AE97FCD", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/AD9037CD-0DDE-403F-9F6A-53908AE97FCD/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Alex J", "profile_url": "https://stats.stackexchange.com/users/369002/alex-j", "user_type": "registered"}, "created_at": "2026-08-28T01:38:43+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "3EAA59CF-9ACC-4B3D-9AFA-AB5D8ECE34B4", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/3EAA59CF-9ACC-4B3D-9AFA-AB5D8ECE34B4/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "User1865345", "profile_url": "https://stats.stackexchange.com/users/362671/user1865345", "user_type": "registered"}, "created_at": "2026-08-28T04:20:19+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "7E6B4854-2230-4F24-8E0A-5B62915E43E4", "revision_number": 3, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/7E6B4854-2230-4F24-8E0A-5B62915E43E4/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Dodo", "profile_url": "https://stats.stackexchange.com/users/516003/dodo", "user_type": "registered"}, "created_at": "2026-08-28T15:01:14+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "F19DD4EF-9466-40CB-B4D0-9CE189FEB3F1", "revision_number": 4, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/F19DD4EF-9466-40CB-B4D0-9CE189FEB3F1/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Dodo", "profile_url": "https://stats.stackexchange.com/users/516003/dodo", "user_type": "registered"}, "created_at": "2026-08-28T15:09:40+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "007496D1-0D4A-482B-BAD6-609D5895915F", "revision_number": 5, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/007496D1-0D4A-482B-BAD6-609D5895915F/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Dodo", "profile_url": "https://stats.stackexchange.com/users/516003/dodo", "user_type": "registered"}, "created_at": "2026-08-28T20:29:14+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "12B957FD-E529-454A-B047-188DDFB56FEC", "revision_number": 6, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/12B957FD-E529-454A-B047-188DDFB56FEC/view-source"}, {"content_license": null, "contributor": {"display_name": "[deleted/unavailable user]", "profile_url": null, "user_type": "does_not_exist"}, "created_at": "2026-08-29T05:37:00+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "4E87FA39-5CD2-406D-9D24-0B402CCCA345", "revision_number": null, "revision_type": "vote_based", "revision_url": "https://stats.stackexchange.com/revisions/4E87FA39-5CD2-406D-9D24-0B402CCCA345/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Dodo", "profile_url": "https://stats.stackexchange.com/users/516003/dodo", "user_type": "registered"}, "created_at": "2026-08-29T22:21:59+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "E05011F7-55D9-4351-B560-D118A78E2098", "revision_number": 7, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/E05011F7-55D9-4351-B560-D118A78E2098/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Dodo", "profile_url": "https://stats.stackexchange.com/users/516003/dodo", "user_type": "registered"}, "created_at": "2026-08-29T23:47:46+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "2A145D07-0EC6-4F2F-846F-61F813A88425", "revision_number": 8, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/2A145D07-0EC6-4F2F-846F-61F813A88425/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Dodo", "profile_url": "https://stats.stackexchange.com/users/516003/dodo", "user_type": "registered"}, "created_at": "2026-08-30T08:44:53+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "9A297BC1-B323-4435-8926-2848718059A9", "revision_number": 9, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/9A297BC1-B323-4435-8926-2848718059A9/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "User1865345", "profile_url": "https://stats.stackexchange.com/users/362671/user1865345", "user_type": "registered"}, "created_at": "2026-08-30T09:13:27+00:00", "raw_file": "raw/codex_api_v1/84bb317e707465a80f008019404ef1f770e152d74ef692d3b81c48903ec855cc_1790825232001984700_0.json", "raw_sha256": "75778d86d5325e15f4b491a748dc70ae6ac6ee98840d584e01ed28416cb71fbe", "revision_guid": "67969179-E863-4693-B637-9E30F0487804", "revision_number": 10, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/67969179-E863-4693-B637-9E30F0487804/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/676982/glmmtmb-with-beta-distribution-and-repeated-measures-with-high-r-squared-too-g", "split": "validation", "split_group": "4a3fc879ae3d375972ec508b97fe4ec07ecf984d1615b6ef74597b17b4827d41", "tags": ["mixed-model", "repeated-measures", "glmm", "beta-distribution", "glmmtmb"], "thread_id": "stats:676982", "title": "glmmTMB with beta distribution and repeated measures with high R-squared - too good to be true"}}
{"accepted_answer": {"answer_html": "<p>Confidence interval procedures already account for the sample size. A good 95% CI procedure should cover the parameter 95% of the time, regardless of <span class=\"math-container\">$n$</span>, by definition. If <span class=\"math-container\">$n$</span> is smaller, the CIs will just tend to be wider.<br />\n(...of course, with the exception of discrete-data situations where at certain <span class=\"math-container\">$n$</span> you can get, let's say, 93% or 97% coverage but not exactly 95%, etc.)</p>\n<p>So when choosing <span class=\"math-container\">$n$</span>, you shouldn't need to focus much on your 2nd case. Instead you can focus on your 1st case, which sounds to me like &quot;Accuracy in Parameter Estimation&quot; (AIPE). See for example <a href=\"https://pubmed.ncbi.nlm.nih.gov/12971200/\" rel=\"nofollow noreferrer\">Kelley, Maxwell, and Rausch (2003, &quot;Obtaining power or obtaining precision. Delineating methods of sample-size planning&quot;</a>.</p>\n<p>(Finally, since you ask what's &quot;typical,&quot; the most common situation in practice is neither of your 2 cases. Instead, the most common framework for choosing <span class=\"math-container\">$n$</span> is to have adequate statistical power, in a hypothesis testing setting. But that is partly a historical quirk of how statistics was developed and taught. There are good reasons to focus on things like AIPE instead of hypothesis testing.)</p>\n", "answer_id": 677102, "answer_text": "Confidence interval procedures already account for the sample size. A good 95% CI procedure should cover the parameter 95% of the time, regardless of $n$, by definition. If $n$ is smaller, the CIs will just tend to be wider.\n\n(...of course, with the exception of discrete-data situations where at certain $n$ you can get, let's say, 93% or 97% coverage but not exactly 95%, etc.)\n\n\n\n\nSo when choosing $n$, you shouldn't need to focus much on your 2nd case. Instead you can focus on your 1st case, which sounds to me like \"Accuracy in Parameter Estimation\" (AIPE). See for example Kelley, Maxwell, and Rausch (2003, \"Obtaining power or obtaining precision. Delineating methods of sample-size planning\" (https://pubmed.ncbi.nlm.nih.gov/12971200/).\n\n\n\n\n(Finally, since you ask what's \"typical,\" the most common situation in practice is neither of your 2 cases. Instead, the most common framework for choosing $n$ is to have adequate statistical power, in a hypothesis testing setting. But that is partly a historical quirk of how statistics was developed and taught. There are good reasons to focus on things like AIPE instead of hypothesis testing.)", "answer_url": "https://stats.stackexchange.com/a/677102", "author": "civilstat", "author_url": "https://stats.stackexchange.com/users/17414/civilstat", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T13:23:08+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677098, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "civilstat", "profile_url": "https://stats.stackexchange.com/users/17414/civilstat", "user_type": "registered"}, "created_at": "2026-09-08T13:23:08+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "30E58C39-F28E-4090-A7B7-0EC9CE56FFCB", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/30E58C39-F28E-4090-A7B7-0EC9CE56FFCB/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "civilstat", "profile_url": "https://stats.stackexchange.com/users/17414/civilstat", "user_type": "registered"}, "created_at": "2026-09-15T01:01:34+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "D313A7E5-AE58-48F9-A18F-C0F2150BB098", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/D313A7E5-AE58-48F9-A18F-C0F2150BB098/view-source"}], "score": 5, "updated_at": "2026-09-15T01:01:34+00:00"}, "other_answers": [], "product": "statistics_qa", "question": "Given a statistical model $M(\\theta_1,\\dots,\\theta_p)$.\n\n\n\n\nAssuming a true value of each $\\theta_i$ is known, via simulation, I can determine two different sample sizes:\n\n\n\n\n\n\n\nParameter Recovery: The $n$ needed so that each estimate $\\hat\\theta_i$ falls within $\\pm k\\%$ of its true value with probability at least $q$ .\n\n\n\n\n\n\n\n\n\nCoverage: The $n$ needed so that a 95% confidence interval for the estimated mean of $M$ (i.e. $\\mathbb{E}_M[Y]$) contains the true mean a proportion $j$ of the time (coverage of the derived quantity $g(\\theta) = \\mathbb{E}_M[Y]$).\n\n\n\n\n\n\n\n\nWhich of these is typically the more important factor/target when choosing $n$?", "record_id": "Scientific-Statistics-QA:stats:677098", "selected_answer": {"answer_html": "<p>Confidence interval procedures already account for the sample size. A good 95% CI procedure should cover the parameter 95% of the time, regardless of <span class=\"math-container\">$n$</span>, by definition. If <span class=\"math-container\">$n$</span> is smaller, the CIs will just tend to be wider.<br />\n(...of course, with the exception of discrete-data situations where at certain <span class=\"math-container\">$n$</span> you can get, let's say, 93% or 97% coverage but not exactly 95%, etc.)</p>\n<p>So when choosing <span class=\"math-container\">$n$</span>, you shouldn't need to focus much on your 2nd case. Instead you can focus on your 1st case, which sounds to me like &quot;Accuracy in Parameter Estimation&quot; (AIPE). See for example <a href=\"https://pubmed.ncbi.nlm.nih.gov/12971200/\" rel=\"nofollow noreferrer\">Kelley, Maxwell, and Rausch (2003, &quot;Obtaining power or obtaining precision. Delineating methods of sample-size planning&quot;</a>.</p>\n<p>(Finally, since you ask what's &quot;typical,&quot; the most common situation in practice is neither of your 2 cases. Instead, the most common framework for choosing <span class=\"math-container\">$n$</span> is to have adequate statistical power, in a hypothesis testing setting. But that is partly a historical quirk of how statistics was developed and taught. There are good reasons to focus on things like AIPE instead of hypothesis testing.)</p>\n", "answer_id": 677102, "answer_text": "Confidence interval procedures already account for the sample size. A good 95% CI procedure should cover the parameter 95% of the time, regardless of $n$, by definition. If $n$ is smaller, the CIs will just tend to be wider.\n\n(...of course, with the exception of discrete-data situations where at certain $n$ you can get, let's say, 93% or 97% coverage but not exactly 95%, etc.)\n\n\n\n\nSo when choosing $n$, you shouldn't need to focus much on your 2nd case. Instead you can focus on your 1st case, which sounds to me like \"Accuracy in Parameter Estimation\" (AIPE). See for example Kelley, Maxwell, and Rausch (2003, \"Obtaining power or obtaining precision. Delineating methods of sample-size planning\" (https://pubmed.ncbi.nlm.nih.gov/12971200/).\n\n\n\n\n(Finally, since you ask what's \"typical,\" the most common situation in practice is neither of your 2 cases. Instead, the most common framework for choosing $n$ is to have adequate statistical power, in a hypothesis testing setting. But that is partly a historical quirk of how statistics was developed and taught. There are good reasons to focus on things like AIPE instead of hypothesis testing.)", "answer_url": "https://stats.stackexchange.com/a/677102", "author": "civilstat", "author_url": "https://stats.stackexchange.com/users/17414/civilstat", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T13:23:08+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677098, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "civilstat", "profile_url": "https://stats.stackexchange.com/users/17414/civilstat", "user_type": "registered"}, "created_at": "2026-09-08T13:23:08+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "30E58C39-F28E-4090-A7B7-0EC9CE56FFCB", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/30E58C39-F28E-4090-A7B7-0EC9CE56FFCB/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "civilstat", "profile_url": "https://stats.stackexchange.com/users/17414/civilstat", "user_type": "registered"}, "created_at": "2026-09-15T01:01:34+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "D313A7E5-AE58-48F9-A18F-C0F2150BB098", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/D313A7E5-AE58-48F9-A18F-C0F2150BB098/view-source"}], "score": 5, "updated_at": "2026-09-15T01:01:34+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": 677102, "answers": [{"answer_html": "<p>Confidence interval procedures already account for the sample size. A good 95% CI procedure should cover the parameter 95% of the time, regardless of <span class=\"math-container\">$n$</span>, by definition. If <span class=\"math-container\">$n$</span> is smaller, the CIs will just tend to be wider.<br />\n(...of course, with the exception of discrete-data situations where at certain <span class=\"math-container\">$n$</span> you can get, let's say, 93% or 97% coverage but not exactly 95%, etc.)</p>\n<p>So when choosing <span class=\"math-container\">$n$</span>, you shouldn't need to focus much on your 2nd case. Instead you can focus on your 1st case, which sounds to me like &quot;Accuracy in Parameter Estimation&quot; (AIPE). See for example <a href=\"https://pubmed.ncbi.nlm.nih.gov/12971200/\" rel=\"nofollow noreferrer\">Kelley, Maxwell, and Rausch (2003, &quot;Obtaining power or obtaining precision. Delineating methods of sample-size planning&quot;</a>.</p>\n<p>(Finally, since you ask what's &quot;typical,&quot; the most common situation in practice is neither of your 2 cases. Instead, the most common framework for choosing <span class=\"math-container\">$n$</span> is to have adequate statistical power, in a hypothesis testing setting. But that is partly a historical quirk of how statistics was developed and taught. There are good reasons to focus on things like AIPE instead of hypothesis testing.)</p>\n", "answer_id": 677102, "answer_text": "Confidence interval procedures already account for the sample size. A good 95% CI procedure should cover the parameter 95% of the time, regardless of $n$, by definition. If $n$ is smaller, the CIs will just tend to be wider.\n\n(...of course, with the exception of discrete-data situations where at certain $n$ you can get, let's say, 93% or 97% coverage but not exactly 95%, etc.)\n\n\n\n\nSo when choosing $n$, you shouldn't need to focus much on your 2nd case. Instead you can focus on your 1st case, which sounds to me like \"Accuracy in Parameter Estimation\" (AIPE). See for example Kelley, Maxwell, and Rausch (2003, \"Obtaining power or obtaining precision. Delineating methods of sample-size planning\" (https://pubmed.ncbi.nlm.nih.gov/12971200/).\n\n\n\n\n(Finally, since you ask what's \"typical,\" the most common situation in practice is neither of your 2 cases. Instead, the most common framework for choosing $n$ is to have adequate statistical power, in a hypothesis testing setting. But that is partly a historical quirk of how statistics was developed and taught. There are good reasons to focus on things like AIPE instead of hypothesis testing.)", "answer_url": "https://stats.stackexchange.com/a/677102", "author": "civilstat", "author_url": "https://stats.stackexchange.com/users/17414/civilstat", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T13:23:08+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677098, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "civilstat", "profile_url": "https://stats.stackexchange.com/users/17414/civilstat", "user_type": "registered"}, "created_at": "2026-09-08T13:23:08+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "30E58C39-F28E-4090-A7B7-0EC9CE56FFCB", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/30E58C39-F28E-4090-A7B7-0EC9CE56FFCB/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "civilstat", "profile_url": "https://stats.stackexchange.com/users/17414/civilstat", "user_type": "registered"}, "created_at": "2026-09-15T01:01:34+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "D313A7E5-AE58-48F9-A18F-C0F2150BB098", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/D313A7E5-AE58-48F9-A18F-C0F2150BB098/view-source"}], "score": 5, "updated_at": "2026-09-15T01:01:34+00:00"}], "domain": "statistics", "external_links": ["https://pubmed.ncbi.nlm.nih.gov/12971200/"], "medical_sensitive": false, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "adamkostanov", "question_author_url": "https://stats.stackexchange.com/users/512554/adamkostanov", "question_author_user_type": "registered", "question_created_at": "2026-09-08T04:17:59+00:00", "question_html": "<p>Given a statistical model <span class=\"math-container\">$M(\\theta_1,\\dots,\\theta_p)$</span>.</p>\n<p>Assuming a true value of each <span class=\"math-container\">$\\theta_i$</span> is known, via simulation, I can determine two different sample sizes:</p>\n<ol>\n<li><p>Parameter Recovery: The <span class=\"math-container\">$n$</span> needed so that each estimate <span class=\"math-container\">$\\hat\\theta_i$</span> falls within <span class=\"math-container\">$\\pm k\\%$</span> of its true value with probability at least <span class=\"math-container\">$q$</span> .</p>\n</li>\n<li><p>Coverage: The <span class=\"math-container\">$n$</span> needed so that a 95% confidence interval for the estimated mean of <span class=\"math-container\">$M$</span> (i.e. <span class=\"math-container\">$\\mathbb{E}_M[Y]$</span>) contains the true mean a proportion <span class=\"math-container\">$j$</span> of the time (coverage of the derived quantity <span class=\"math-container\">$g(\\theta) = \\mathbb{E}_M[Y]$</span>).</p>\n</li>\n</ol>\n<p>Which of these is typically the more important factor/target when choosing <span class=\"math-container\">$n$</span>?</p>\n", "question_id": 677098, "question_license": "CC BY-SA 4.0", "question_score": 1, "question_text": "Given a statistical model $M(\\theta_1,\\dots,\\theta_p)$.\n\n\n\n\nAssuming a true value of each $\\theta_i$ is known, via simulation, I can determine two different sample sizes:\n\n\n\n\n\n\n\nParameter Recovery: The $n$ needed so that each estimate $\\hat\\theta_i$ falls within $\\pm k\\%$ of its true value with probability at least $q$ .\n\n\n\n\n\n\n\n\n\nCoverage: The $n$ needed so that a 95% confidence interval for the estimated mean of $M$ (i.e. $\\mathbb{E}_M[Y]$) contains the true mean a proportion $j$ of the time (coverage of the derived quantity $g(\\theta) = \\mathbb{E}_M[Y]$).\n\n\n\n\n\n\n\n\nWhich of these is typically the more important factor/target when choosing $n$?", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "adamkostanov", "profile_url": "https://stats.stackexchange.com/users/512554/adamkostanov", "user_type": "registered"}, "created_at": "2026-09-08T04:17:59+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "15313CED-1404-44EA-A230-82E79875C5B8", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/15313CED-1404-44EA-A230-82E79875C5B8/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "adamkostanov", "profile_url": "https://stats.stackexchange.com/users/512554/adamkostanov", "user_type": "registered"}, "created_at": "2026-09-08T04:23:48+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "3D41642C-3420-4A9F-843C-F98D90590AF5", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/3D41642C-3420-4A9F-843C-F98D90590AF5/view-source"}, {"content_license": null, "contributor": {"display_name": "[deleted/unavailable user]", "profile_url": null, "user_type": "does_not_exist"}, "created_at": "2026-09-09T16:17:18+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "BF6E51A9-985F-4D9C-AFDE-2ABFDAC7C9D7", "revision_number": null, "revision_type": "vote_based", "revision_url": "https://stats.stackexchange.com/revisions/BF6E51A9-985F-4D9C-AFDE-2ABFDAC7C9D7/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/677098/what-factor-is-more-important-in-determining-sample-size", "split": "validation", "split_group": "185fcb021546ad3cd40051f624c4e2d290e2963794c032b8452e062ce2a554d6", "tags": ["sampling"], "thread_id": "stats:677098", "title": "What factor is more important in determining sample size?"}}
{"accepted_answer": {"answer_html": "<p>Using a Weibull model will not help: as you say, the Weibull model is a proportional hazards model.</p>\n<p>It could help to use other accelerated failure models, eg, lognormal. Whether this works will depend on what the failure in proportional hazards looks like.</p>\n<p>At one extreme: if all the hazard ratios show a similar pattern of increase or decrease over time then a different underlying model might fix the problem</p>\n<p>At the other: if the hazard ratio for some covariates is roughly constant over time but the hazard ratio for other covariates decreases strongly, changing the underlying model won't help -- you need to model time-dependent effects. This sort of thing happens fairly frequently in medical data: some measurements get 'stale', so that a year-old measurement of (say) clotting time doesn't tell you much about current risk. Others don't: if someone was old or low-income or had a history of smoking a year ago, the measurement is still just as valid as it was.</p>\n<p>You can look at changes in the hazard ratio over time with smoothed Schoenfeld residuals -- in R, this is <code>survival::plot.cox.zph</code></p>\n", "answer_id": 677106, "answer_text": "Using a Weibull model will not help: as you say, the Weibull model is a proportional hazards model.\n\n\n\n\nIt could help to use other accelerated failure models, eg, lognormal. Whether this works will depend on what the failure in proportional hazards looks like.\n\n\n\n\nAt one extreme: if all the hazard ratios show a similar pattern of increase or decrease over time then a different underlying model might fix the problem\n\n\n\n\nAt the other: if the hazard ratio for some covariates is roughly constant over time but the hazard ratio for other covariates decreases strongly, changing the underlying model won't help -- you need to model time-dependent effects. This sort of thing happens fairly frequently in medical data: some measurements get 'stale', so that a year-old measurement of (say) clotting time doesn't tell you much about current risk. Others don't: if someone was old or low-income or had a history of smoking a year ago, the measurement is still just as valid as it was.\n\n\n\n\nYou can look at changes in the hazard ratio over time with smoothed Schoenfeld residuals -- in R, this is survival::plot.cox.zph", "answer_url": "https://stats.stackexchange.com/a/677106", "author": "Thomas Lumley", "author_url": "https://stats.stackexchange.com/users/249135/thomas-lumley", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T21:28:29+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677099, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas Lumley", "profile_url": "https://stats.stackexchange.com/users/249135/thomas-lumley", "user_type": "registered"}, "created_at": "2026-09-08T21:28:29+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "084444E7-448C-4715-9B8D-97651E34E425", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/084444E7-448C-4715-9B8D-97651E34E425/view-source"}], "score": 8, "updated_at": "2026-09-08T21:28:29+00:00"}, "other_answers": [{"answer_html": "<p>One big advantage of the Cox PH model is that it does not make assumptions about the baseline hazard function, where parametric models do, with the Weibull model assuming the baseline hazard follows a Weibull distribution. Unfortunately, that is an assumption and, if violated, the results can be very misleading.</p>\n<p>If you go the parametric route and do not have good substantive reasons to choose one distribution, it is common to try several and choose based on such criteria as AIC.</p>\n<p>Other ways of dealing with violation of the PH assumption (including some discussion of when the violation may not matter) are covered in <a href=\"https://stats.stackexchange.com/questions/631309/violation-of-cox-proportional-hazard-assumption\">this thread</a> and its subthreads and probably other threads on CV.</p>\n", "answer_id": 677100, "answer_text": "One big advantage of the Cox PH model is that it does not make assumptions about the baseline hazard function, where parametric models do, with the Weibull model assuming the baseline hazard follows a Weibull distribution. Unfortunately, that is an assumption and, if violated, the results can be very misleading.\n\n\n\n\nIf you go the parametric route and do not have good substantive reasons to choose one distribution, it is common to try several and choose based on such criteria as AIC.\n\n\n\n\nOther ways of dealing with violation of the PH assumption (including some discussion of when the violation may not matter) are covered in this thread (https://stats.stackexchange.com/questions/631309/violation-of-cox-proportional-hazard-assumption) and its subthreads and probably other threads on CV.", "answer_url": "https://stats.stackexchange.com/a/677100", "author": "Peter Flom", "author_url": "https://stats.stackexchange.com/users/686/peter-flom", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T10:43:47+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677099, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Peter Flom", "profile_url": "https://stats.stackexchange.com/users/686/peter-flom", "user_type": "registered"}, "created_at": "2026-09-08T10:43:47+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "7A56B279-E283-4665-B1AD-FBECCC69008E", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/7A56B279-E283-4665-B1AD-FBECCC69008E/view-source"}], "score": 8, "updated_at": "2026-09-08T10:43:47+00:00"}], "product": "statistics_qa", "question": "I am using survival analysis to analyze unemployment duration from a panel dataset. I would like to control for covariates, and hence I decided to use a Cox PH Model. However, after fitting model, I run the PH test using cox.zph() in R. All my covariates have corresponding p-value of $< 2 \\times 10^{-16}$. I tried stratifying using various variables, but that only marginally improves the p-value for some covariates.\n\n\n\n\nI am considering using different models now. I was suggested to use the Weibull model, but I am not sure if this will work as Weibull model also follows the PH assumption. So my questions is:\n\n\n\n\ni) Is there any point trying to use a Weibull model?\n\n\n\n\nii) What other models should I consider using?", "record_id": "Scientific-Statistics-QA:stats:677099", "selected_answer": {"answer_html": "<p>Using a Weibull model will not help: as you say, the Weibull model is a proportional hazards model.</p>\n<p>It could help to use other accelerated failure models, eg, lognormal. Whether this works will depend on what the failure in proportional hazards looks like.</p>\n<p>At one extreme: if all the hazard ratios show a similar pattern of increase or decrease over time then a different underlying model might fix the problem</p>\n<p>At the other: if the hazard ratio for some covariates is roughly constant over time but the hazard ratio for other covariates decreases strongly, changing the underlying model won't help -- you need to model time-dependent effects. This sort of thing happens fairly frequently in medical data: some measurements get 'stale', so that a year-old measurement of (say) clotting time doesn't tell you much about current risk. Others don't: if someone was old or low-income or had a history of smoking a year ago, the measurement is still just as valid as it was.</p>\n<p>You can look at changes in the hazard ratio over time with smoothed Schoenfeld residuals -- in R, this is <code>survival::plot.cox.zph</code></p>\n", "answer_id": 677106, "answer_text": "Using a Weibull model will not help: as you say, the Weibull model is a proportional hazards model.\n\n\n\n\nIt could help to use other accelerated failure models, eg, lognormal. Whether this works will depend on what the failure in proportional hazards looks like.\n\n\n\n\nAt one extreme: if all the hazard ratios show a similar pattern of increase or decrease over time then a different underlying model might fix the problem\n\n\n\n\nAt the other: if the hazard ratio for some covariates is roughly constant over time but the hazard ratio for other covariates decreases strongly, changing the underlying model won't help -- you need to model time-dependent effects. This sort of thing happens fairly frequently in medical data: some measurements get 'stale', so that a year-old measurement of (say) clotting time doesn't tell you much about current risk. Others don't: if someone was old or low-income or had a history of smoking a year ago, the measurement is still just as valid as it was.\n\n\n\n\nYou can look at changes in the hazard ratio over time with smoothed Schoenfeld residuals -- in R, this is survival::plot.cox.zph", "answer_url": "https://stats.stackexchange.com/a/677106", "author": "Thomas Lumley", "author_url": "https://stats.stackexchange.com/users/249135/thomas-lumley", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T21:28:29+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677099, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas Lumley", "profile_url": "https://stats.stackexchange.com/users/249135/thomas-lumley", "user_type": "registered"}, "created_at": "2026-09-08T21:28:29+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "084444E7-448C-4715-9B8D-97651E34E425", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/084444E7-448C-4715-9B8D-97651E34E425/view-source"}], "score": 8, "updated_at": "2026-09-08T21:28:29+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": 677106, "answers": [{"answer_html": "<p>One big advantage of the Cox PH model is that it does not make assumptions about the baseline hazard function, where parametric models do, with the Weibull model assuming the baseline hazard follows a Weibull distribution. Unfortunately, that is an assumption and, if violated, the results can be very misleading.</p>\n<p>If you go the parametric route and do not have good substantive reasons to choose one distribution, it is common to try several and choose based on such criteria as AIC.</p>\n<p>Other ways of dealing with violation of the PH assumption (including some discussion of when the violation may not matter) are covered in <a href=\"https://stats.stackexchange.com/questions/631309/violation-of-cox-proportional-hazard-assumption\">this thread</a> and its subthreads and probably other threads on CV.</p>\n", "answer_id": 677100, "answer_text": "One big advantage of the Cox PH model is that it does not make assumptions about the baseline hazard function, where parametric models do, with the Weibull model assuming the baseline hazard follows a Weibull distribution. Unfortunately, that is an assumption and, if violated, the results can be very misleading.\n\n\n\n\nIf you go the parametric route and do not have good substantive reasons to choose one distribution, it is common to try several and choose based on such criteria as AIC.\n\n\n\n\nOther ways of dealing with violation of the PH assumption (including some discussion of when the violation may not matter) are covered in this thread (https://stats.stackexchange.com/questions/631309/violation-of-cox-proportional-hazard-assumption) and its subthreads and probably other threads on CV.", "answer_url": "https://stats.stackexchange.com/a/677100", "author": "Peter Flom", "author_url": "https://stats.stackexchange.com/users/686/peter-flom", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T10:43:47+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677099, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Peter Flom", "profile_url": "https://stats.stackexchange.com/users/686/peter-flom", "user_type": "registered"}, "created_at": "2026-09-08T10:43:47+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "7A56B279-E283-4665-B1AD-FBECCC69008E", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/7A56B279-E283-4665-B1AD-FBECCC69008E/view-source"}], "score": 8, "updated_at": "2026-09-08T10:43:47+00:00"}, {"answer_html": "<p>Using a Weibull model will not help: as you say, the Weibull model is a proportional hazards model.</p>\n<p>It could help to use other accelerated failure models, eg, lognormal. Whether this works will depend on what the failure in proportional hazards looks like.</p>\n<p>At one extreme: if all the hazard ratios show a similar pattern of increase or decrease over time then a different underlying model might fix the problem</p>\n<p>At the other: if the hazard ratio for some covariates is roughly constant over time but the hazard ratio for other covariates decreases strongly, changing the underlying model won't help -- you need to model time-dependent effects. This sort of thing happens fairly frequently in medical data: some measurements get 'stale', so that a year-old measurement of (say) clotting time doesn't tell you much about current risk. Others don't: if someone was old or low-income or had a history of smoking a year ago, the measurement is still just as valid as it was.</p>\n<p>You can look at changes in the hazard ratio over time with smoothed Schoenfeld residuals -- in R, this is <code>survival::plot.cox.zph</code></p>\n", "answer_id": 677106, "answer_text": "Using a Weibull model will not help: as you say, the Weibull model is a proportional hazards model.\n\n\n\n\nIt could help to use other accelerated failure models, eg, lognormal. Whether this works will depend on what the failure in proportional hazards looks like.\n\n\n\n\nAt one extreme: if all the hazard ratios show a similar pattern of increase or decrease over time then a different underlying model might fix the problem\n\n\n\n\nAt the other: if the hazard ratio for some covariates is roughly constant over time but the hazard ratio for other covariates decreases strongly, changing the underlying model won't help -- you need to model time-dependent effects. This sort of thing happens fairly frequently in medical data: some measurements get 'stale', so that a year-old measurement of (say) clotting time doesn't tell you much about current risk. Others don't: if someone was old or low-income or had a history of smoking a year ago, the measurement is still just as valid as it was.\n\n\n\n\nYou can look at changes in the hazard ratio over time with smoothed Schoenfeld residuals -- in R, this is survival::plot.cox.zph", "answer_url": "https://stats.stackexchange.com/a/677106", "author": "Thomas Lumley", "author_url": "https://stats.stackexchange.com/users/249135/thomas-lumley", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-08T21:28:29+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:06.169765+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/9155f81b0f1dac4b0f83fad58469033af46ad34ee8d731e569d063b41b3df6b6_1790825226563497500_0.json", "raw_sha256": "cbac4b1b24e2159f1c17ea9902eaf89a0a5260344e6dba6f75790ba955f0d04b", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677099, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas Lumley", "profile_url": "https://stats.stackexchange.com/users/249135/thomas-lumley", "user_type": "registered"}, "created_at": "2026-09-08T21:28:29+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "084444E7-448C-4715-9B8D-97651E34E425", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/084444E7-448C-4715-9B8D-97651E34E425/view-source"}], "score": 8, "updated_at": "2026-09-08T21:28:29+00:00"}], "domain": "statistics", "external_links": [], "medical_sensitive": false, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "Jyotiraditya Pradhan", "question_author_url": "https://stats.stackexchange.com/users/516391/jyotiraditya-pradhan", "question_author_user_type": "registered", "question_created_at": "2026-09-08T09:32:39+00:00", "question_html": "<p>I am using survival analysis to analyze unemployment duration from a panel dataset. I would like to control for covariates, and hence I decided to use a Cox PH Model. However, after fitting model, I run the PH test using <code>cox.zph()</code> in R. All my covariates have corresponding p-value of <span class=\"math-container\">$&lt; 2 \\times 10^{-16}$</span>. I tried stratifying using various variables, but that only marginally improves the p-value for some covariates.</p>\n<p>I am considering using different models now. I was suggested to use the Weibull model, but I am not sure if this will work as Weibull model also follows the PH assumption. So my questions is:</p>\n<p>i) Is there any point trying to use a Weibull model?</p>\n<p>ii) What other models should I consider using?</p>\n", "question_id": 677099, "question_license": "CC BY-SA 4.0", "question_score": 4, "question_text": "I am using survival analysis to analyze unemployment duration from a panel dataset. I would like to control for covariates, and hence I decided to use a Cox PH Model. However, after fitting model, I run the PH test using cox.zph() in R. All my covariates have corresponding p-value of $< 2 \\times 10^{-16}$. I tried stratifying using various variables, but that only marginally improves the p-value for some covariates.\n\n\n\n\nI am considering using different models now. I was suggested to use the Weibull model, but I am not sure if this will work as Weibull model also follows the PH assumption. So my questions is:\n\n\n\n\ni) Is there any point trying to use a Weibull model?\n\n\n\n\nii) What other models should I consider using?", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Jyotiraditya Pradhan", "profile_url": "https://stats.stackexchange.com/users/516391/jyotiraditya-pradhan", "user_type": "registered"}, "created_at": "2026-09-08T09:32:39+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "CB9BCE6F-95A0-421A-95ED-573B1B48D672", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/CB9BCE6F-95A0-421A-95ED-573B1B48D672/view-source"}, {"content_license": null, "contributor": {"display_name": "[deleted/unavailable user]", "profile_url": null, "user_type": "does_not_exist"}, "created_at": "2026-09-08T17:46:34+00:00", "raw_file": "raw/codex_api_v1/3f959895a29b33d83894e5370d82e1c0cf87f20ac57fa423ebc923738357c657_1790825253957066500_0.json", "raw_sha256": "ddd8808039e410ca99f97e2a7f12c307f046c4e5c3e184683bc12f816e6f49fc", "revision_guid": "526A8939-CD1A-4CBE-9348-A46579D30E48", "revision_number": null, "revision_type": "vote_based", "revision_url": "https://stats.stackexchange.com/revisions/526A8939-CD1A-4CBE-9348-A46579D30E48/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/677099/if-ph-assumption-is-failing-when-using-cox-ph-model-is-using-weibull-helpful", "split": "validation", "split_group": "6da6bb4e3c33593ced4d29574089843aded6ada336baf0e13fea5ac0dd464d18", "tags": ["survival", "cox-model", "weibull-distribution"], "thread_id": "stats:677099", "title": "If PH assumption is failing when using Cox-PH model, is using Weibull helpful?"}}
{"accepted_answer": {"answer_html": "<p>Fix treatment arm <span class=\"math-container\">$d$</span> and, to keep the notation light, let all sums and expectations below be taken within that arm. Write</p>\n<p><span class=\"math-container\">$$\n\\widehat{\\tau}\n=\n\\frac{\\overline{Y}}{\\overline{M}}\n=\n\\frac{\\sum_i Y_i}{\\sum_i M_i},\n\\qquad\n\\tau\n=\n\\frac{E[Y]}{E[M]}.\n$$</span></p>\n<p>A first-order Taylor expansion of <span class=\"math-container\">$f(y,m)=y/m$</span> gives</p>\n<p><span class=\"math-container\">$$\n\\begin{aligned}\n\\widehat{\\tau}-\\tau\n&amp;\\approx\n\\frac{\\overline{Y}-E[Y]}{E[M]}\n-\n\\frac{E[Y]}{E[M]^2}\n\\left(\\overline{M}-E[M]\\right) \\\\[4pt]\n&amp;=\n\\frac{1}{E[M]}\n\\left[\n\\left(\\overline{Y}-E[Y]\\right)\n-\n\\tau\\left(\\overline{M}-E[M]\\right)\n\\right] \\\\[4pt]\n&amp;=\n\\frac{1}{nE[M]}\n\\sum_i\\left(Y_i-\\tau M_i\\right).\n\\end{aligned}\n$$</span></p>\n<p>The last equality uses <span class=\"math-container\">$E[Y]=\\tau E[M]$</span>. Therefore, the influence function is</p>\n<p><span class=\"math-container\">$$\n\\operatorname{IF}_i\n=\n\\frac{Y_i-\\tau M_i}{E[M]}.\n$$</span></p>\n<p>Replacing the unknown quantities with their sample estimates gives</p>\n<p><span class=\"math-container\">$$\n\\widehat{\\operatorname{IF}}_i\n=\n\\frac{Y_i-\\widehat{\\tau}M_i}{\\overline{M}}.\n$$</span></p>\n<p>Thus, apart from the constant scaling factor <span class=\"math-container\">$1/\\overline{M}$</span>, the linearized outcome is</p>\n<p><span class=\"math-container\">$$\nZ_i=Y_i-\\widehat{\\tau}M_i.\n$$</span></p>\n<p>Practically speaking, you can run OLS on the ratios and weight them by the number of trials. I'd use robust covariance in this event too</p>\n<pre class=\"lang-r prettyprint-override\"><code>library(sandwich)\n\nset.seed(0)\ncef &lt;- function(x, d) {\n plogis(-2 + 0.8 * x + 0.3 * d)\n}\n\ntau &lt;- integrate(\n f = function(x) (cef(x, 1) - cef(x, 0)) * dnorm(x, 0, 1),\n lower = -Inf,\n upper = Inf\n)$value\n\nn &lt;- 1000\nsims &lt;- replicate(50000, {\n x1 &lt;- rnorm(n)\n d &lt;- rbinom(n, 1, 0.5)\n M &lt;- pmax(1, rpois(n, 10))\n y &lt;- rbinom(n, M, cef(x1, d))\n r &lt;- y / M\n \n fit &lt;- lm(r ~ x1 + d, weights = M)\n \n est &lt;- coef(fit)[[&quot;d&quot;]]\n se &lt;- sqrt(sandwich::vcovHC(fit, type = &quot;HC0&quot;)[[&quot;d&quot;, &quot;d&quot;]])\n \n lower &lt;- est - qnorm(0.975) * se\n upper &lt;- est + qnorm(0.975) * se\n \n c(\n estimate = est,\n se = se,\n lower = lower,\n upper = upper,\n error = est - tau\n )\n})\n\nsims &lt;- t(sims)\n\nc(\n mean_error = mean(sims[, &quot;error&quot;]),\n coverage = mean(\n sims[, &quot;lower&quot;] &lt;= tau &amp;\n tau &lt;= sims[, &quot;upper&quot;]\n )\n)\n#&gt; mean_error coverage \n#&gt; -5.571571e-05 9.514200e-01\n</code></pre>\n<p><sup>Created on 2026-09-15 with <a href=\"https://reprex.tidyverse.org\" rel=\"noreferrer\">reprex v2.1.1</a></sup></p>\n", "answer_id": 677181, "answer_text": "Fix treatment arm $d$ and, to keep the notation light, let all sums and expectations below be taken within that arm. Write\n\n\n\n\n$$\n\\widehat{\\tau}\n=\n\\frac{\\overline{Y}}{\\overline{M}}\n=\n\\frac{\\sum_i Y_i}{\\sum_i M_i},\n\\qquad\n\\tau\n=\n\\frac{E[Y]}{E[M]}.\n$$\n\n\n\n\nA first-order Taylor expansion of $f(y,m)=y/m$ gives\n\n\n\n\n$$\n\\begin{aligned}\n\\widehat{\\tau}-\\tau\n&\\approx\n\\frac{\\overline{Y}-E[Y]}{E[M]}\n-\n\\frac{E[Y]}{E[M]^2}\n\\left(\\overline{M}-E[M]\\right) \\\\[4pt]\n&=\n\\frac{1}{E[M]}\n\\left[\n\\left(\\overline{Y}-E[Y]\\right)\n-\n\\tau\\left(\\overline{M}-E[M]\\right)\n\\right] \\\\[4pt]\n&=\n\\frac{1}{nE[M]}\n\\sum_i\\left(Y_i-\\tau M_i\\right).\n\\end{aligned}\n$$\n\n\n\n\nThe last equality uses $E[Y]=\\tau E[M]$. Therefore, the influence function is\n\n\n\n\n$$\n\\operatorname{IF}_i\n=\n\\frac{Y_i-\\tau M_i}{E[M]}.\n$$\n\n\n\n\nReplacing the unknown quantities with their sample estimates gives\n\n\n\n\n$$\n\\widehat{\\operatorname{IF}}_i\n=\n\\frac{Y_i-\\widehat{\\tau}M_i}{\\overline{M}}.\n$$\n\n\n\n\nThus, apart from the constant scaling factor $1/\\overline{M}$, the linearized outcome is\n\n\n\n\n$$\nZ_i=Y_i-\\widehat{\\tau}M_i.\n$$\n\n\n\n\nPractically speaking, you can run OLS on the ratios and weight them by the number of trials. I'd use robust covariance in this event too\n\n\n\n\nlibrary(sandwich)\n\nset.seed(0)\ncef <- function(x, d) {\n plogis(-2 + 0.8 * x + 0.3 * d)\n}\n\ntau <- integrate(\n f = function(x) (cef(x, 1) - cef(x, 0)) * dnorm(x, 0, 1),\n lower = -Inf,\n upper = Inf\n)$value\n\nn <- 1000\nsims <- replicate(50000, {\n x1 <- rnorm(n)\n d <- rbinom(n, 1, 0.5)\n M <- pmax(1, rpois(n, 10))\n y <- rbinom(n, M, cef(x1, d))\n r <- y / M\n \n fit <- lm(r ~ x1 + d, weights = M)\n \n est <- coef(fit)[[\"d\"]]\n se <- sqrt(sandwich::vcovHC(fit, type = \"HC0\")[[\"d\", \"d\"]])\n \n lower <- est - qnorm(0.975) * se\n upper <- est + qnorm(0.975) * se\n \n c(\n estimate = est,\n se = se,\n lower = lower,\n upper = upper,\n error = est - tau\n )\n})\n\nsims <- t(sims)\n\nc(\n mean_error = mean(sims[, \"error\"]),\n coverage = mean(\n sims[, \"lower\"] <= tau &\n tau <= sims[, \"upper\"]\n )\n)\n#> mean_error coverage \n#> -5.571571e-05 9.514200e-01\n\n\n\n\n\nCreated on 2026-09-15 with reprex v2.1.1 (https://reprex.tidyverse.org)", "answer_url": "https://stats.stackexchange.com/a/677181", "author": "Demetri Pananos", "author_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-15T16:27:34+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:08.569053+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/ba8a114171cbec551f0a7f4b9f04e95c559131a62357d51e159434d4acc7183d_1790825228829630100_0.json", "raw_sha256": "a03822ca860dd0b0fb0cc402754ae4cdbaad51d18873dfd19aed56ffe1cf7ec8", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=2&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677179, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Demetri Pananos", "profile_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "user_type": "registered"}, "created_at": "2026-09-15T16:27:34+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "AC30AD7E-7B7F-4D55-B05C-76A70A413D78", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/AC30AD7E-7B7F-4D55-B05C-76A70A413D78/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Demetri Pananos", "profile_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "user_type": "registered"}, "created_at": "2026-09-15T16:35:13+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "755739D7-7C88-4D82-AD57-75C2AE33AFBC", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/755739D7-7C88-4D82-AD57-75C2AE33AFBC/view-source"}], "score": 6, "updated_at": "2026-09-15T16:35:13+00:00"}, "other_answers": [], "product": "statistics_qa", "question": "In my workplace, I came across an idea that OLS can be retooled to solve ratio metric inference problems.\n\n\n\n\nWhen I think of $k_i$ successes out of $m_i$ trials for individual $i$, this seems to be a perfect use case for binomial regression. Where your chief interest is estimating the conditional probability of success. Its strength, natively handling non-linearity, is also its weakness: Extracting the marginal probability of success is a nontrivial operation; marginalization via G-computation would be needed:\n$$logit(K=k|M=m, X=x, d=1) - logit(K=k|M=m, X=x, d=0)$$\n\n\n\n\nAnd this is a large computational burden to assume with millions or billions of observations. So, I've been pointed to the OLS solution, which I understand to be based on the \"delta method\", correcting the linear solution with gradient information to accommodate curvature in the nonlinearity (ratio function), through the Taylor Series Expansion.\n\n\n\n\nNaively, we have two options for OLS.\n\n\n\n\nFirst, infer in the ratio space directly. But this approach completely mutes the number of trials and biases inference when $corr(K, M)$ exists.\n\n\n\n\n$$ \\frac{k_i}{m_i} = \\alpha + \\lambda d_i + \\beta X +\\epsilon $$\n\n\n\n\nThe second naive option is to infer the difference of global ratios directly where $D_j$ is the binary design vector for treatment exposure.This is equally problematic due to a sample size of one.\n\n\n\n\n$$ \\frac{\\sum_{j=1} K_j D_j}{\\sum_{j=1} M_j D_j} - \\frac{\\sum_{j=1} K_j (1-D_j)}{\\sum_{j=1} M_j (1-D_j)} = \\alpha + \\lambda d_i + \\beta X +\\epsilon $$\n\n\n\n\nThe end solution seems to address the bias shortcoming in naive approach 1 and the variance shortcoming in naive solution 2; it has two parts, computing $z_i$ as the difference between the actual successes and expected successes, and regressing $z_i$ via OLS given treatment exposure among other covariates.\n\n\n\n\n$$ z_i = k_i - (\\frac{\\sum_{j=1} K_j}{\\sum_{j=1} M_j}) m_i $$\n\n\n\n\n$$ z_i = \\alpha + \\lambda d_i + \\beta X_i +\\epsilon $$\n\n\n\n\nIf all of the above, is correct, and that's a big if, where I'm lost at is seeing the connection between the $z_i$ form and the Taylor Series Expansion.\n\n\n\n\nQuestion: How does the TSE prove $z_i$ to be the correct solution accounting for the nonlinearity in the ratio function?", "record_id": "Scientific-Statistics-QA:stats:677179", "selected_answer": {"answer_html": "<p>Fix treatment arm <span class=\"math-container\">$d$</span> and, to keep the notation light, let all sums and expectations below be taken within that arm. Write</p>\n<p><span class=\"math-container\">$$\n\\widehat{\\tau}\n=\n\\frac{\\overline{Y}}{\\overline{M}}\n=\n\\frac{\\sum_i Y_i}{\\sum_i M_i},\n\\qquad\n\\tau\n=\n\\frac{E[Y]}{E[M]}.\n$$</span></p>\n<p>A first-order Taylor expansion of <span class=\"math-container\">$f(y,m)=y/m$</span> gives</p>\n<p><span class=\"math-container\">$$\n\\begin{aligned}\n\\widehat{\\tau}-\\tau\n&amp;\\approx\n\\frac{\\overline{Y}-E[Y]}{E[M]}\n-\n\\frac{E[Y]}{E[M]^2}\n\\left(\\overline{M}-E[M]\\right) \\\\[4pt]\n&amp;=\n\\frac{1}{E[M]}\n\\left[\n\\left(\\overline{Y}-E[Y]\\right)\n-\n\\tau\\left(\\overline{M}-E[M]\\right)\n\\right] \\\\[4pt]\n&amp;=\n\\frac{1}{nE[M]}\n\\sum_i\\left(Y_i-\\tau M_i\\right).\n\\end{aligned}\n$$</span></p>\n<p>The last equality uses <span class=\"math-container\">$E[Y]=\\tau E[M]$</span>. Therefore, the influence function is</p>\n<p><span class=\"math-container\">$$\n\\operatorname{IF}_i\n=\n\\frac{Y_i-\\tau M_i}{E[M]}.\n$$</span></p>\n<p>Replacing the unknown quantities with their sample estimates gives</p>\n<p><span class=\"math-container\">$$\n\\widehat{\\operatorname{IF}}_i\n=\n\\frac{Y_i-\\widehat{\\tau}M_i}{\\overline{M}}.\n$$</span></p>\n<p>Thus, apart from the constant scaling factor <span class=\"math-container\">$1/\\overline{M}$</span>, the linearized outcome is</p>\n<p><span class=\"math-container\">$$\nZ_i=Y_i-\\widehat{\\tau}M_i.\n$$</span></p>\n<p>Practically speaking, you can run OLS on the ratios and weight them by the number of trials. I'd use robust covariance in this event too</p>\n<pre class=\"lang-r prettyprint-override\"><code>library(sandwich)\n\nset.seed(0)\ncef &lt;- function(x, d) {\n plogis(-2 + 0.8 * x + 0.3 * d)\n}\n\ntau &lt;- integrate(\n f = function(x) (cef(x, 1) - cef(x, 0)) * dnorm(x, 0, 1),\n lower = -Inf,\n upper = Inf\n)$value\n\nn &lt;- 1000\nsims &lt;- replicate(50000, {\n x1 &lt;- rnorm(n)\n d &lt;- rbinom(n, 1, 0.5)\n M &lt;- pmax(1, rpois(n, 10))\n y &lt;- rbinom(n, M, cef(x1, d))\n r &lt;- y / M\n \n fit &lt;- lm(r ~ x1 + d, weights = M)\n \n est &lt;- coef(fit)[[&quot;d&quot;]]\n se &lt;- sqrt(sandwich::vcovHC(fit, type = &quot;HC0&quot;)[[&quot;d&quot;, &quot;d&quot;]])\n \n lower &lt;- est - qnorm(0.975) * se\n upper &lt;- est + qnorm(0.975) * se\n \n c(\n estimate = est,\n se = se,\n lower = lower,\n upper = upper,\n error = est - tau\n )\n})\n\nsims &lt;- t(sims)\n\nc(\n mean_error = mean(sims[, &quot;error&quot;]),\n coverage = mean(\n sims[, &quot;lower&quot;] &lt;= tau &amp;\n tau &lt;= sims[, &quot;upper&quot;]\n )\n)\n#&gt; mean_error coverage \n#&gt; -5.571571e-05 9.514200e-01\n</code></pre>\n<p><sup>Created on 2026-09-15 with <a href=\"https://reprex.tidyverse.org\" rel=\"noreferrer\">reprex v2.1.1</a></sup></p>\n", "answer_id": 677181, "answer_text": "Fix treatment arm $d$ and, to keep the notation light, let all sums and expectations below be taken within that arm. Write\n\n\n\n\n$$\n\\widehat{\\tau}\n=\n\\frac{\\overline{Y}}{\\overline{M}}\n=\n\\frac{\\sum_i Y_i}{\\sum_i M_i},\n\\qquad\n\\tau\n=\n\\frac{E[Y]}{E[M]}.\n$$\n\n\n\n\nA first-order Taylor expansion of $f(y,m)=y/m$ gives\n\n\n\n\n$$\n\\begin{aligned}\n\\widehat{\\tau}-\\tau\n&\\approx\n\\frac{\\overline{Y}-E[Y]}{E[M]}\n-\n\\frac{E[Y]}{E[M]^2}\n\\left(\\overline{M}-E[M]\\right) \\\\[4pt]\n&=\n\\frac{1}{E[M]}\n\\left[\n\\left(\\overline{Y}-E[Y]\\right)\n-\n\\tau\\left(\\overline{M}-E[M]\\right)\n\\right] \\\\[4pt]\n&=\n\\frac{1}{nE[M]}\n\\sum_i\\left(Y_i-\\tau M_i\\right).\n\\end{aligned}\n$$\n\n\n\n\nThe last equality uses $E[Y]=\\tau E[M]$. Therefore, the influence function is\n\n\n\n\n$$\n\\operatorname{IF}_i\n=\n\\frac{Y_i-\\tau M_i}{E[M]}.\n$$\n\n\n\n\nReplacing the unknown quantities with their sample estimates gives\n\n\n\n\n$$\n\\widehat{\\operatorname{IF}}_i\n=\n\\frac{Y_i-\\widehat{\\tau}M_i}{\\overline{M}}.\n$$\n\n\n\n\nThus, apart from the constant scaling factor $1/\\overline{M}$, the linearized outcome is\n\n\n\n\n$$\nZ_i=Y_i-\\widehat{\\tau}M_i.\n$$\n\n\n\n\nPractically speaking, you can run OLS on the ratios and weight them by the number of trials. I'd use robust covariance in this event too\n\n\n\n\nlibrary(sandwich)\n\nset.seed(0)\ncef <- function(x, d) {\n plogis(-2 + 0.8 * x + 0.3 * d)\n}\n\ntau <- integrate(\n f = function(x) (cef(x, 1) - cef(x, 0)) * dnorm(x, 0, 1),\n lower = -Inf,\n upper = Inf\n)$value\n\nn <- 1000\nsims <- replicate(50000, {\n x1 <- rnorm(n)\n d <- rbinom(n, 1, 0.5)\n M <- pmax(1, rpois(n, 10))\n y <- rbinom(n, M, cef(x1, d))\n r <- y / M\n \n fit <- lm(r ~ x1 + d, weights = M)\n \n est <- coef(fit)[[\"d\"]]\n se <- sqrt(sandwich::vcovHC(fit, type = \"HC0\")[[\"d\", \"d\"]])\n \n lower <- est - qnorm(0.975) * se\n upper <- est + qnorm(0.975) * se\n \n c(\n estimate = est,\n se = se,\n lower = lower,\n upper = upper,\n error = est - tau\n )\n})\n\nsims <- t(sims)\n\nc(\n mean_error = mean(sims[, \"error\"]),\n coverage = mean(\n sims[, \"lower\"] <= tau &\n tau <= sims[, \"upper\"]\n )\n)\n#> mean_error coverage \n#> -5.571571e-05 9.514200e-01\n\n\n\n\n\nCreated on 2026-09-15 with reprex v2.1.1 (https://reprex.tidyverse.org)", "answer_url": "https://stats.stackexchange.com/a/677181", "author": "Demetri Pananos", "author_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-15T16:27:34+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:08.569053+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/ba8a114171cbec551f0a7f4b9f04e95c559131a62357d51e159434d4acc7183d_1790825228829630100_0.json", "raw_sha256": "a03822ca860dd0b0fb0cc402754ae4cdbaad51d18873dfd19aed56ffe1cf7ec8", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=2&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677179, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Demetri Pananos", "profile_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "user_type": "registered"}, "created_at": "2026-09-15T16:27:34+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "AC30AD7E-7B7F-4D55-B05C-76A70A413D78", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/AC30AD7E-7B7F-4D55-B05C-76A70A413D78/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Demetri Pananos", "profile_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "user_type": "registered"}, "created_at": "2026-09-15T16:35:13+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "755739D7-7C88-4D82-AD57-75C2AE33AFBC", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/755739D7-7C88-4D82-AD57-75C2AE33AFBC/view-source"}], "score": 6, "updated_at": "2026-09-15T16:35:13+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": 677181, "answers": [{"answer_html": "<p>Fix treatment arm <span class=\"math-container\">$d$</span> and, to keep the notation light, let all sums and expectations below be taken within that arm. Write</p>\n<p><span class=\"math-container\">$$\n\\widehat{\\tau}\n=\n\\frac{\\overline{Y}}{\\overline{M}}\n=\n\\frac{\\sum_i Y_i}{\\sum_i M_i},\n\\qquad\n\\tau\n=\n\\frac{E[Y]}{E[M]}.\n$$</span></p>\n<p>A first-order Taylor expansion of <span class=\"math-container\">$f(y,m)=y/m$</span> gives</p>\n<p><span class=\"math-container\">$$\n\\begin{aligned}\n\\widehat{\\tau}-\\tau\n&amp;\\approx\n\\frac{\\overline{Y}-E[Y]}{E[M]}\n-\n\\frac{E[Y]}{E[M]^2}\n\\left(\\overline{M}-E[M]\\right) \\\\[4pt]\n&amp;=\n\\frac{1}{E[M]}\n\\left[\n\\left(\\overline{Y}-E[Y]\\right)\n-\n\\tau\\left(\\overline{M}-E[M]\\right)\n\\right] \\\\[4pt]\n&amp;=\n\\frac{1}{nE[M]}\n\\sum_i\\left(Y_i-\\tau M_i\\right).\n\\end{aligned}\n$$</span></p>\n<p>The last equality uses <span class=\"math-container\">$E[Y]=\\tau E[M]$</span>. Therefore, the influence function is</p>\n<p><span class=\"math-container\">$$\n\\operatorname{IF}_i\n=\n\\frac{Y_i-\\tau M_i}{E[M]}.\n$$</span></p>\n<p>Replacing the unknown quantities with their sample estimates gives</p>\n<p><span class=\"math-container\">$$\n\\widehat{\\operatorname{IF}}_i\n=\n\\frac{Y_i-\\widehat{\\tau}M_i}{\\overline{M}}.\n$$</span></p>\n<p>Thus, apart from the constant scaling factor <span class=\"math-container\">$1/\\overline{M}$</span>, the linearized outcome is</p>\n<p><span class=\"math-container\">$$\nZ_i=Y_i-\\widehat{\\tau}M_i.\n$$</span></p>\n<p>Practically speaking, you can run OLS on the ratios and weight them by the number of trials. I'd use robust covariance in this event too</p>\n<pre class=\"lang-r prettyprint-override\"><code>library(sandwich)\n\nset.seed(0)\ncef &lt;- function(x, d) {\n plogis(-2 + 0.8 * x + 0.3 * d)\n}\n\ntau &lt;- integrate(\n f = function(x) (cef(x, 1) - cef(x, 0)) * dnorm(x, 0, 1),\n lower = -Inf,\n upper = Inf\n)$value\n\nn &lt;- 1000\nsims &lt;- replicate(50000, {\n x1 &lt;- rnorm(n)\n d &lt;- rbinom(n, 1, 0.5)\n M &lt;- pmax(1, rpois(n, 10))\n y &lt;- rbinom(n, M, cef(x1, d))\n r &lt;- y / M\n \n fit &lt;- lm(r ~ x1 + d, weights = M)\n \n est &lt;- coef(fit)[[&quot;d&quot;]]\n se &lt;- sqrt(sandwich::vcovHC(fit, type = &quot;HC0&quot;)[[&quot;d&quot;, &quot;d&quot;]])\n \n lower &lt;- est - qnorm(0.975) * se\n upper &lt;- est + qnorm(0.975) * se\n \n c(\n estimate = est,\n se = se,\n lower = lower,\n upper = upper,\n error = est - tau\n )\n})\n\nsims &lt;- t(sims)\n\nc(\n mean_error = mean(sims[, &quot;error&quot;]),\n coverage = mean(\n sims[, &quot;lower&quot;] &lt;= tau &amp;\n tau &lt;= sims[, &quot;upper&quot;]\n )\n)\n#&gt; mean_error coverage \n#&gt; -5.571571e-05 9.514200e-01\n</code></pre>\n<p><sup>Created on 2026-09-15 with <a href=\"https://reprex.tidyverse.org\" rel=\"noreferrer\">reprex v2.1.1</a></sup></p>\n", "answer_id": 677181, "answer_text": "Fix treatment arm $d$ and, to keep the notation light, let all sums and expectations below be taken within that arm. Write\n\n\n\n\n$$\n\\widehat{\\tau}\n=\n\\frac{\\overline{Y}}{\\overline{M}}\n=\n\\frac{\\sum_i Y_i}{\\sum_i M_i},\n\\qquad\n\\tau\n=\n\\frac{E[Y]}{E[M]}.\n$$\n\n\n\n\nA first-order Taylor expansion of $f(y,m)=y/m$ gives\n\n\n\n\n$$\n\\begin{aligned}\n\\widehat{\\tau}-\\tau\n&\\approx\n\\frac{\\overline{Y}-E[Y]}{E[M]}\n-\n\\frac{E[Y]}{E[M]^2}\n\\left(\\overline{M}-E[M]\\right) \\\\[4pt]\n&=\n\\frac{1}{E[M]}\n\\left[\n\\left(\\overline{Y}-E[Y]\\right)\n-\n\\tau\\left(\\overline{M}-E[M]\\right)\n\\right] \\\\[4pt]\n&=\n\\frac{1}{nE[M]}\n\\sum_i\\left(Y_i-\\tau M_i\\right).\n\\end{aligned}\n$$\n\n\n\n\nThe last equality uses $E[Y]=\\tau E[M]$. Therefore, the influence function is\n\n\n\n\n$$\n\\operatorname{IF}_i\n=\n\\frac{Y_i-\\tau M_i}{E[M]}.\n$$\n\n\n\n\nReplacing the unknown quantities with their sample estimates gives\n\n\n\n\n$$\n\\widehat{\\operatorname{IF}}_i\n=\n\\frac{Y_i-\\widehat{\\tau}M_i}{\\overline{M}}.\n$$\n\n\n\n\nThus, apart from the constant scaling factor $1/\\overline{M}$, the linearized outcome is\n\n\n\n\n$$\nZ_i=Y_i-\\widehat{\\tau}M_i.\n$$\n\n\n\n\nPractically speaking, you can run OLS on the ratios and weight them by the number of trials. I'd use robust covariance in this event too\n\n\n\n\nlibrary(sandwich)\n\nset.seed(0)\ncef <- function(x, d) {\n plogis(-2 + 0.8 * x + 0.3 * d)\n}\n\ntau <- integrate(\n f = function(x) (cef(x, 1) - cef(x, 0)) * dnorm(x, 0, 1),\n lower = -Inf,\n upper = Inf\n)$value\n\nn <- 1000\nsims <- replicate(50000, {\n x1 <- rnorm(n)\n d <- rbinom(n, 1, 0.5)\n M <- pmax(1, rpois(n, 10))\n y <- rbinom(n, M, cef(x1, d))\n r <- y / M\n \n fit <- lm(r ~ x1 + d, weights = M)\n \n est <- coef(fit)[[\"d\"]]\n se <- sqrt(sandwich::vcovHC(fit, type = \"HC0\")[[\"d\", \"d\"]])\n \n lower <- est - qnorm(0.975) * se\n upper <- est + qnorm(0.975) * se\n \n c(\n estimate = est,\n se = se,\n lower = lower,\n upper = upper,\n error = est - tau\n )\n})\n\nsims <- t(sims)\n\nc(\n mean_error = mean(sims[, \"error\"]),\n coverage = mean(\n sims[, \"lower\"] <= tau &\n tau <= sims[, \"upper\"]\n )\n)\n#> mean_error coverage \n#> -5.571571e-05 9.514200e-01\n\n\n\n\n\nCreated on 2026-09-15 with reprex v2.1.1 (https://reprex.tidyverse.org)", "answer_url": "https://stats.stackexchange.com/a/677181", "author": "Demetri Pananos", "author_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-15T16:27:34+00:00", "is_accepted": true, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:08.569053+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/ba8a114171cbec551f0a7f4b9f04e95c559131a62357d51e159434d4acc7183d_1790825228829630100_0.json", "raw_sha256": "a03822ca860dd0b0fb0cc402754ae4cdbaad51d18873dfd19aed56ffe1cf7ec8", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=2&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677179, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Demetri Pananos", "profile_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "user_type": "registered"}, "created_at": "2026-09-15T16:27:34+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "AC30AD7E-7B7F-4D55-B05C-76A70A413D78", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/AC30AD7E-7B7F-4D55-B05C-76A70A413D78/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Demetri Pananos", "profile_url": "https://stats.stackexchange.com/users/111259/demetri-pananos", "user_type": "registered"}, "created_at": "2026-09-15T16:35:13+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "755739D7-7C88-4D82-AD57-75C2AE33AFBC", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/755739D7-7C88-4D82-AD57-75C2AE33AFBC/view-source"}], "score": 6, "updated_at": "2026-09-15T16:35:13+00:00"}], "domain": "statistics", "external_links": ["https://reprex.tidyverse.org"], "medical_sensitive": true, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "jbuddy_13", "question_author_url": "https://stats.stackexchange.com/users/288172/jbuddy-13", "question_author_user_type": "registered", "question_created_at": "2026-09-15T15:25:50+00:00", "question_html": "<p>In my workplace, I came across an idea that OLS can be retooled to solve ratio metric inference problems.</p>\n<p>When I think of <span class=\"math-container\">$k_i$</span> successes out of <span class=\"math-container\">$m_i$</span> trials for individual <span class=\"math-container\">$i$</span>, this seems to be a perfect use case for binomial regression. Where your chief interest is estimating the conditional probability of success. Its strength, natively handling non-linearity, is also its weakness: Extracting the marginal probability of success is a nontrivial operation; marginalization via G-computation would be needed:\n<span class=\"math-container\">$$logit(K=k|M=m, X=x, d=1) - logit(K=k|M=m, X=x, d=0)$$</span></p>\n<p>And this is a large computational burden to assume with millions or billions of observations. So, I've been pointed to the OLS solution, which I understand to be based on the &quot;delta method&quot;, correcting the linear solution with gradient information to accommodate curvature in the nonlinearity (ratio function), through the Taylor Series Expansion.</p>\n<p>Naively, we have two options for OLS.</p>\n<p>First, infer in the ratio space directly. But this approach completely mutes the number of trials and biases inference when <span class=\"math-container\">$corr(K, M)$</span> exists.</p>\n<p><span class=\"math-container\">$$ \\frac{k_i}{m_i} = \\alpha + \\lambda d_i + \\beta X +\\epsilon $$</span></p>\n<p>The second naive option is to infer the difference of global ratios directly where <span class=\"math-container\">$D_j$</span> is the binary design vector for treatment exposure.This is equally problematic due to a sample size of one.</p>\n<p><span class=\"math-container\">$$ \\frac{\\sum_{j=1} K_j D_j}{\\sum_{j=1} M_j D_j} - \\frac{\\sum_{j=1} K_j (1-D_j)}{\\sum_{j=1} M_j (1-D_j)} = \\alpha + \\lambda d_i + \\beta X +\\epsilon $$</span></p>\n<p>The end solution seems to address the bias shortcoming in naive approach 1 and the variance shortcoming in naive solution 2; it has two parts, computing <span class=\"math-container\">$z_i$</span> as the difference between the actual successes and expected successes, and regressing <span class=\"math-container\">$z_i$</span> via OLS given treatment exposure among other covariates.</p>\n<p><span class=\"math-container\">$$ z_i = k_i - (\\frac{\\sum_{j=1} K_j}{\\sum_{j=1} M_j}) m_i $$</span></p>\n<p><span class=\"math-container\">$$ z_i = \\alpha + \\lambda d_i + \\beta X_i +\\epsilon $$</span></p>\n<p>If all of the above, is correct, and that's a <strong>big if</strong>, where I'm lost at is seeing the connection between the <span class=\"math-container\">$z_i$</span> form and the Taylor Series Expansion.</p>\n<p><strong>Question</strong>: <em>How does the TSE prove <span class=\"math-container\">$z_i$</span> to be the correct solution accounting for the nonlinearity in the ratio function?</em></p>\n", "question_id": 677179, "question_license": "CC BY-SA 4.0", "question_score": 3, "question_text": "In my workplace, I came across an idea that OLS can be retooled to solve ratio metric inference problems.\n\n\n\n\nWhen I think of $k_i$ successes out of $m_i$ trials for individual $i$, this seems to be a perfect use case for binomial regression. Where your chief interest is estimating the conditional probability of success. Its strength, natively handling non-linearity, is also its weakness: Extracting the marginal probability of success is a nontrivial operation; marginalization via G-computation would be needed:\n$$logit(K=k|M=m, X=x, d=1) - logit(K=k|M=m, X=x, d=0)$$\n\n\n\n\nAnd this is a large computational burden to assume with millions or billions of observations. So, I've been pointed to the OLS solution, which I understand to be based on the \"delta method\", correcting the linear solution with gradient information to accommodate curvature in the nonlinearity (ratio function), through the Taylor Series Expansion.\n\n\n\n\nNaively, we have two options for OLS.\n\n\n\n\nFirst, infer in the ratio space directly. But this approach completely mutes the number of trials and biases inference when $corr(K, M)$ exists.\n\n\n\n\n$$ \\frac{k_i}{m_i} = \\alpha + \\lambda d_i + \\beta X +\\epsilon $$\n\n\n\n\nThe second naive option is to infer the difference of global ratios directly where $D_j$ is the binary design vector for treatment exposure.This is equally problematic due to a sample size of one.\n\n\n\n\n$$ \\frac{\\sum_{j=1} K_j D_j}{\\sum_{j=1} M_j D_j} - \\frac{\\sum_{j=1} K_j (1-D_j)}{\\sum_{j=1} M_j (1-D_j)} = \\alpha + \\lambda d_i + \\beta X +\\epsilon $$\n\n\n\n\nThe end solution seems to address the bias shortcoming in naive approach 1 and the variance shortcoming in naive solution 2; it has two parts, computing $z_i$ as the difference between the actual successes and expected successes, and regressing $z_i$ via OLS given treatment exposure among other covariates.\n\n\n\n\n$$ z_i = k_i - (\\frac{\\sum_{j=1} K_j}{\\sum_{j=1} M_j}) m_i $$\n\n\n\n\n$$ z_i = \\alpha + \\lambda d_i + \\beta X_i +\\epsilon $$\n\n\n\n\nIf all of the above, is correct, and that's a big if, where I'm lost at is seeing the connection between the $z_i$ form and the Taylor Series Expansion.\n\n\n\n\nQuestion: How does the TSE prove $z_i$ to be the correct solution accounting for the nonlinearity in the ratio function?", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "jbuddy_13", "profile_url": "https://stats.stackexchange.com/users/288172/jbuddy-13", "user_type": "registered"}, "created_at": "2026-09-15T15:25:50+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "E477C4E5-8119-4D6D-8EB7-06E7F1147CE1", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/E477C4E5-8119-4D6D-8EB7-06E7F1147CE1/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "jbuddy_13", "profile_url": "https://stats.stackexchange.com/users/288172/jbuddy-13", "user_type": "registered"}, "created_at": "2026-09-15T15:51:31+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "7C5055B2-E916-464C-9092-9B0B51019D05", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/7C5055B2-E916-464C-9092-9B0B51019D05/view-source"}, {"content_license": null, "contributor": {"display_name": "[deleted/unavailable user]", "profile_url": null, "user_type": "does_not_exist"}, "created_at": "2026-09-15T23:37:38+00:00", "raw_file": "raw/codex_api_v1/59f98009db0ac38325a869c60e3993040b07f5fbd07014051415831400b70d25_1790825251781774200_0.json", "raw_sha256": "2876465103b1eeb5d593dd46b1ec52a862c1da72f8e4767e5f8ee70ff0564119", "revision_guid": "0015EC33-1DEB-456C-8D58-1E9522CF13C1", "revision_number": null, "revision_type": "vote_based", "revision_url": "https://stats.stackexchange.com/revisions/0015EC33-1DEB-456C-8D58-1E9522CF13C1/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/677179/delta-method-for-linearizing-success-rate-inference", "split": "validation", "split_group": "bb14fd5ee1c3dd00639ebb6fa31a6ba50a54e417aaf3c07d0873f8be08a6ced7", "tags": ["logistic", "binomial-distribution", "delta-method"], "thread_id": "stats:677179", "title": "Delta method for linearizing success rate inference"}}
{"accepted_answer": null, "other_answers": [], "product": "statistics_qa", "question": "[image: enter image description here; source: https://i.sstatic.net/Lh0AOcud.png] (https://i.sstatic.net/Lh0AOcud.png)\n\n\n\n\nI have two polynomial fits of the same dataset: a second-order fit and a third-order fit. The second-order fit has a single extreme point, and the third-order fit has two extreme points.\n\n\n\n\nI would like to compare the first local extreme of these two fits. That is: I am trying to find a statistical test that would allow me to know whether the x-value of these extreme points are the same, or significantly different.\n\n\n\n\nVisualization above. Remember, these are both fitted to the same set of data.", "record_id": "Scientific-Statistics-QA:stats:677225", "selected_answer": {"answer_html": "<p>As the 2 posts linked by @whuber in his comments allude to, such a task appears quite challenging. I will provide a possible approach, but I cannot vouch for its validity. I would be cautious in any case, as such extrema can be quite noisy, would be substantially influenced by high-leverage points, may not occur in the range of x-values from your dataset, etc.<br />\nThis being said, if I had to do what you want to do, (“<em>find a statistical test that would allow me to know whether the x-value of these extrem[a are] significantly different</em>), and assuming that the dataset is sufficiently large (? maybe <span class=\"math-container\">$n&gt;50$</span> ?), I would use bootstrapping.</p>\n<p>Take a sample from the original dataset, of same size <span class=\"math-container\">$n$</span>, <strong>with replacement</strong>. Fit a quadratic and cubic to this resample; derive the 2 extrema of interest (0’s of the 1st derivative) and compute <span class=\"math-container\">$\\Delta X$</span>, the difference in x-values between them. Repeat the above a large (? 10,000 ?) number of times. Find the desired quantiles of these many <span class=\"math-container\">$\\Delta X$</span>’s (e.g. 2.5% and 97.5 for the “traditional” 5% significance level, or some other suitable significance). If the observed <span class=\"math-container\">$\\Delta X_0$</span> is outside these 2 quantiles, you can reject the null that the difference in x-values was due to pure chance (i.e. that the observed difference would arise simply from the random differences between datasets from your population). The R code to do this bootstrap should not be too hard to write (the functions boot, and boot.ci should help a lot).</p>\n", "answer_id": 677230, "answer_text": "As the 2 posts linked by @whuber in his comments allude to, such a task appears quite challenging. I will provide a possible approach, but I cannot vouch for its validity. I would be cautious in any case, as such extrema can be quite noisy, would be substantially influenced by high-leverage points, may not occur in the range of x-values from your dataset, etc.\n\nThis being said, if I had to do what you want to do, (“find a statistical test that would allow me to know whether the x-value of these extrem[a are] significantly different), and assuming that the dataset is sufficiently large (? maybe $n>50$ ?), I would use bootstrapping.\n\n\n\n\nTake a sample from the original dataset, of same size $n$, with replacement. Fit a quadratic and cubic to this resample; derive the 2 extrema of interest (0’s of the 1st derivative) and compute $\\Delta X$, the difference in x-values between them. Repeat the above a large (? 10,000 ?) number of times. Find the desired quantiles of these many $\\Delta X$’s (e.g. 2.5% and 97.5 for the “traditional” 5% significance level, or some other suitable significance). If the observed $\\Delta X_0$ is outside these 2 quantiles, you can reject the null that the difference in x-values was due to pure chance (i.e. that the observed difference would arise simply from the random differences between datasets from your population). The R code to do this bootstrap should not be too hard to write (the functions boot, and boot.ci should help a lot).", "answer_url": "https://stats.stackexchange.com/a/677230", "author": "jginestet", "author_url": "https://stats.stackexchange.com/users/380096/jginestet", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-21T20:36:48+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:08.569053+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/ba8a114171cbec551f0a7f4b9f04e95c559131a62357d51e159434d4acc7183d_1790825228829630100_0.json", "raw_sha256": "a03822ca860dd0b0fb0cc402754ae4cdbaad51d18873dfd19aed56ffe1cf7ec8", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=2&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677225, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "jginestet", "profile_url": "https://stats.stackexchange.com/users/380096/jginestet", "user_type": "registered"}, "created_at": "2026-09-21T20:36:48+00:00", "raw_file": "raw/codex_api_v1/ab5f4cd7f0a10bde5970354a9b213efcc81e232042487437e8579705d4ca6a20_1790825258916612700_0.json", "raw_sha256": "4cf167c399d03b9ae013b65260fcb4e38139c42cfc70aba4807f070550c1768e", "revision_guid": "4F10EF81-2804-478F-8F37-0E73D3975734", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/4F10EF81-2804-478F-8F37-0E73D3975734/view-source"}], "score": 3, "updated_at": "2026-09-21T20:36:48+00:00"}, "selection_rule": "accepted; otherwise maximum score >= 1, tie lowest ID; no correctness label", "split": "validation", "thread": {"accepted_answer_id": null, "answers": [{"answer_html": "<p>As the 2 posts linked by @whuber in his comments allude to, such a task appears quite challenging. I will provide a possible approach, but I cannot vouch for its validity. I would be cautious in any case, as such extrema can be quite noisy, would be substantially influenced by high-leverage points, may not occur in the range of x-values from your dataset, etc.<br />\nThis being said, if I had to do what you want to do, (“<em>find a statistical test that would allow me to know whether the x-value of these extrem[a are] significantly different</em>), and assuming that the dataset is sufficiently large (? maybe <span class=\"math-container\">$n&gt;50$</span> ?), I would use bootstrapping.</p>\n<p>Take a sample from the original dataset, of same size <span class=\"math-container\">$n$</span>, <strong>with replacement</strong>. Fit a quadratic and cubic to this resample; derive the 2 extrema of interest (0’s of the 1st derivative) and compute <span class=\"math-container\">$\\Delta X$</span>, the difference in x-values between them. Repeat the above a large (? 10,000 ?) number of times. Find the desired quantiles of these many <span class=\"math-container\">$\\Delta X$</span>’s (e.g. 2.5% and 97.5 for the “traditional” 5% significance level, or some other suitable significance). If the observed <span class=\"math-container\">$\\Delta X_0$</span> is outside these 2 quantiles, you can reject the null that the difference in x-values was due to pure chance (i.e. that the observed difference would arise simply from the random differences between datasets from your population). The R code to do this bootstrap should not be too hard to write (the functions boot, and boot.ci should help a lot).</p>\n", "answer_id": 677230, "answer_text": "As the 2 posts linked by @whuber in his comments allude to, such a task appears quite challenging. I will provide a possible approach, but I cannot vouch for its validity. I would be cautious in any case, as such extrema can be quite noisy, would be substantially influenced by high-leverage points, may not occur in the range of x-values from your dataset, etc.\n\nThis being said, if I had to do what you want to do, (“find a statistical test that would allow me to know whether the x-value of these extrem[a are] significantly different), and assuming that the dataset is sufficiently large (? maybe $n>50$ ?), I would use bootstrapping.\n\n\n\n\nTake a sample from the original dataset, of same size $n$, with replacement. Fit a quadratic and cubic to this resample; derive the 2 extrema of interest (0’s of the 1st derivative) and compute $\\Delta X$, the difference in x-values between them. Repeat the above a large (? 10,000 ?) number of times. Find the desired quantiles of these many $\\Delta X$’s (e.g. 2.5% and 97.5 for the “traditional” 5% significance level, or some other suitable significance). If the observed $\\Delta X_0$ is outside these 2 quantiles, you can reject the null that the difference in x-values was due to pure chance (i.e. that the observed difference would arise simply from the random differences between datasets from your population). The R code to do this bootstrap should not be too hard to write (the functions boot, and boot.ci should help a lot).", "answer_url": "https://stats.stackexchange.com/a/677230", "author": "jginestet", "author_url": "https://stats.stackexchange.com/users/380096/jginestet", "author_user_type": "registered", "content_license": "CC BY-SA 4.0", "created_at": "2026-09-21T20:36:48+00:00", "is_accepted": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:08.569053+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/ba8a114171cbec551f0a7f4b9f04e95c559131a62357d51e159434d4acc7183d_1790825228829630100_0.json", "raw_sha256": "a03822ca860dd0b0fb0cc402754ae4cdbaad51d18873dfd19aed56ffe1cf7ec8", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/questions/677298;677295;677280;677279;677278;677275;677269;677266;677262;677256;677245;677243;677242;677226;677225;677217;677211;677201;677194;677193;677186;677183;677179;677177;677171;677169;677168;677151;677149;677147;677137;677135;677131;677129;677115;677110;677109;677101;677099;677098;677096;677095;677094;677085;677083;677079;677078;677075;677071;677066;677065;677062;677058;677045;677041;677035;677033;677023;677021;676999;676997;676991;676982;676981;676979;676977;676975;676971;676966;676961;676958;676956;676952;676947;676945;676937;676935;676933;676924;676922;676899;676898;676893;676889;676888;676879;676874;676873;676870;676867;676865;676858;676855;676850;676842;676832;676830;676824;676823;676821/answers?filter=withbody&order=asc&page=2&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_id": 677225, "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "jginestet", "profile_url": "https://stats.stackexchange.com/users/380096/jginestet", "user_type": "registered"}, "created_at": "2026-09-21T20:36:48+00:00", "raw_file": "raw/codex_api_v1/ab5f4cd7f0a10bde5970354a9b213efcc81e232042487437e8579705d4ca6a20_1790825258916612700_0.json", "raw_sha256": "4cf167c399d03b9ae013b65260fcb4e38139c42cfc70aba4807f070550c1768e", "revision_guid": "4F10EF81-2804-478F-8F37-0E73D3975734", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/4F10EF81-2804-478F-8F37-0E73D3975734/view-source"}], "score": 3, "updated_at": "2026-09-21T20:36:48+00:00"}], "domain": "statistics", "external_links": ["https://i.sstatic.net/Lh0AOcud.png"], "medical_sensitive": false, "patient_specific": false, "provenance": {"attribution_required": true, "collected_at": "2026-10-01T03:27:03.428408+00:00", "license": "CC BY-SA 4.0", "license_url": "https://creativecommons.org/licenses/by-sa/4.0/", "raw_file": "raw/codex_api_v1/774052c18cd9e8fa951e893347bfa3c1a85e743fc46ec4667572002cadb10cbf_1790825224163423600_0.json", "raw_sha256": "fe4dd06d3de6c0b1bb33ba88aee0ed4284118e01f76a58dcdce220a4946329b2", "source_api": "Stack Exchange API 2.3", "source_url": "https://api.stackexchange.com/2.3/search/advanced?answers=1&filter=withbody&order=desc&page=1&pagesize=100&site=stats&sort=creation", "transformation": "API HTML retained; mechanical HTML-to-text; no LLM rewriting"}, "question_author": "Thomas Moore", "question_author_url": "https://stats.stackexchange.com/users/509665/thomas-moore", "question_author_user_type": "registered", "question_created_at": "2026-09-20T20:22:20+00:00", "question_html": "<p><a href=\"https://i.sstatic.net/Lh0AOcud.png\" rel=\"nofollow noreferrer\"><img src=\"https://i.sstatic.net/Lh0AOcud.png\" alt=\"enter image description here\" /></a></p>\n<p>I have two polynomial fits of the same dataset: a second-order fit and a third-order fit. The second-order fit has a single extreme point, and the third-order fit has two extreme points.</p>\n<p>I would like to compare the first local extreme of these two fits. That is: I am trying to find a statistical test that would allow me to know whether the x-value of these extreme points are the same, or significantly different.</p>\n<p>Visualization above. Remember, these are both fitted to the same set of data.</p>\n", "question_id": 677225, "question_license": "CC BY-SA 4.0", "question_score": 3, "question_text": "[image: enter image description here; source: https://i.sstatic.net/Lh0AOcud.png] (https://i.sstatic.net/Lh0AOcud.png)\n\n\n\n\nI have two polynomial fits of the same dataset: a second-order fit and a third-order fit. The second-order fit has a single extreme point, and the third-order fit has two extreme points.\n\n\n\n\nI would like to compare the first local extreme of these two fits. That is: I am trying to find a statistical test that would allow me to know whether the x-value of these extreme points are the same, or significantly different.\n\n\n\n\nVisualization above. Remember, these are both fitted to the same set of data.", "revision_attribution": [{"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas Moore", "profile_url": "https://stats.stackexchange.com/users/509665/thomas-moore", "user_type": "registered"}, "created_at": "2026-09-20T20:22:20+00:00", "raw_file": "raw/codex_api_v1/ab5f4cd7f0a10bde5970354a9b213efcc81e232042487437e8579705d4ca6a20_1790825258916612700_0.json", "raw_sha256": "4cf167c399d03b9ae013b65260fcb4e38139c42cfc70aba4807f070550c1768e", "revision_guid": "8EE5F099-563F-47E6-943F-16BB2E8525DE", "revision_number": 1, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/8EE5F099-563F-47E6-943F-16BB2E8525DE/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas Moore", "profile_url": "https://stats.stackexchange.com/users/509665/thomas-moore", "user_type": "registered"}, "created_at": "2026-09-20T20:28:51+00:00", "raw_file": "raw/codex_api_v1/ab5f4cd7f0a10bde5970354a9b213efcc81e232042487437e8579705d4ca6a20_1790825258916612700_0.json", "raw_sha256": "4cf167c399d03b9ae013b65260fcb4e38139c42cfc70aba4807f070550c1768e", "revision_guid": "A0A723AE-391D-40BC-BBEE-69E6A23BE36C", "revision_number": 2, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/A0A723AE-391D-40BC-BBEE-69E6A23BE36C/view-source"}, {"content_license": "CC BY-SA 4.0", "contributor": {"display_name": "Thomas Moore", "profile_url": "https://stats.stackexchange.com/users/509665/thomas-moore", "user_type": "registered"}, "created_at": "2026-09-20T20:47:09+00:00", "raw_file": "raw/codex_api_v1/ab5f4cd7f0a10bde5970354a9b213efcc81e232042487437e8579705d4ca6a20_1790825258916612700_0.json", "raw_sha256": "4cf167c399d03b9ae013b65260fcb4e38139c42cfc70aba4807f070550c1768e", "revision_guid": "71E71D94-27AF-4E26-B7C7-6F803EBD415F", "revision_number": 3, "revision_type": "single_user", "revision_url": "https://stats.stackexchange.com/revisions/71E71D94-27AF-4E26-B7C7-6F803EBD415F/view-source"}, {"content_license": null, "contributor": {"display_name": "[deleted/unavailable user]", "profile_url": null, "user_type": "does_not_exist"}, "created_at": "2026-09-21T21:12:47+00:00", "raw_file": "raw/codex_api_v1/ab5f4cd7f0a10bde5970354a9b213efcc81e232042487437e8579705d4ca6a20_1790825258916612700_0.json", "raw_sha256": "4cf167c399d03b9ae013b65260fcb4e38139c42cfc70aba4807f070550c1768e", "revision_guid": "FFE6C570-22AB-4B74-895C-BEF572C9421A", "revision_number": null, "revision_type": "vote_based", "revision_url": "https://stats.stackexchange.com/revisions/FFE6C570-22AB-4B74-895C-BEF572C9421A/view-source"}], "source_site": "stats", "source_url": "https://stats.stackexchange.com/questions/677225/comparing-extreme-points-of-two-polynomial-fits-of-the-same-dataset", "split": "validation", "split_group": "28c3e36ae5541869796e6f6aedc890ed37f353f7ffb9886f7ab1d1b570d96777", "tags": ["polynomial"], "thread_id": "stats:677225", "title": "Comparing extreme points of two polynomial fits of the same dataset"}}