{"id":3096,"date":"2026-11-23T09:09:44","date_gmt":"2026-11-23T09:09:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/finance\/?p=3096"},"modified":"2026-11-23T09:09:44","modified_gmt":"2026-11-23T09:09:44","slug":"bring-r-data-and-model-results-into-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/finance\/2026\/11\/23\/bring-r-data-and-model-results-into-matlab\/","title":{"rendered":"Bring R Data and Model Results into MATLAB"},"content":{"rendered":"<h1 style=\"color: #c04f15; font-size: 30px; line-height: 1.25; margin: 0 0 20px;\"><\/h1>\n<p>If you estimate models in R and use MATLAB for forecasting or reporting, you can import the saved data and results directly.<\/p>\n<p><a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/github.com\/mathworks\/matlab-reader-for-r-data-files\" target=\"_blank\" rel=\"noopener noreferrer\">MATLAB Reader for R Data Files<\/a> reads <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">.rds<\/span>, <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">.rda<\/span>, and <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">.RData<\/span> files. It converts R data frames to MATLAB tables and named workspaces to dictionaries. Tables with dates can also be imported as timetables. Import runs entirely in MATLAB.<\/p>\n<p>The worked example below uses public unemployment data to show how to reproduce an R forecast in MATLAB.<\/p>\n<h2 style=\"color: #c04f15; font-size: 22px; line-height: 1.3; margin: 32px 0 12px;\">Install the Reader<\/h2>\n<p>To install from MATLAB Central, open Add-Ons on the MATLAB Home tab. Search for MATLAB Reader for R Data Files, select the reader, and follow the installation prompts.<\/p>\n<p>Alternatively, download <a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/github.com\/mathworks\/matlab-reader-for-r-data-files\/releases\/latest\/download\/rDataReader.mltbx\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">rDataReader.mltbx<\/span> from the latest release<\/a> and open it in MATLAB, or run:<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\"><span style=\"color: #000000;\">matlab<\/span>.<span style=\"color: #000000;\">addons<\/span>.<span style=\"color: #000000;\">install<\/span>(<span style=\"color: #4e9a06;\">\"rDataReader.mltbx\"<\/span>)<\/pre>\n<p>The MATLAB examples require only MATLAB R2025a or later and the reader. R is used to generate the input files.<\/p>\n<p>If you downloaded the source code, run this from the repository folder:<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\"><span style=\"color: #000000;\">addpath<\/span>(<span style=\"color: #000000;\">fullfile<\/span>(<span style=\"color: #000000;\">pwd<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #4e9a06;\">\"tbx\"<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #4e9a06;\">\"rdatareader\"<\/span>))<\/pre>\n<h2 style=\"color: #c04f15; font-size: 22px; line-height: 1.3; margin: 32px 0 12px;\">Read an Economic Dataset<\/h2>\n<p>Start with <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">OECDGrowth<\/span>, a public dataset in the <a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/cran.r-project.org\/package=AER\" target=\"_blank\" rel=\"noopener noreferrer\">Applied Econometrics with R (AER) package<\/a>. It contains 22 observations and six variables for growth regressions.<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\"><span style=\"color: #000000;\">url<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #4e9a06;\">\"https:\/\/raw.githubusercontent.com\/cran\/AER\/master\/data\/OECDGrowth.rda\"<\/span><span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">options<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">weboptions<\/span>(<span style=\"color: #000000;\">Timeout<\/span><span style=\"color: #ce5c00;\"><b>=<\/b><\/span><span style=\"color: #0000cf;\">30<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #000000;\">ContentReader<\/span><span style=\"color: #ce5c00;\"><b>=@<\/b><\/span><span style=\"color: #000000;\">rdata2table<\/span>)<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">T<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">webread<\/span>(<span style=\"color: #000000;\">url<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #000000;\">options<\/span>)<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">head<\/span>(<span style=\"color: #000000;\">T<\/span>)<\/pre>\n<p><span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">webread<\/span> downloads the R file, and <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">rdata2table<\/span> returns a MATLAB table. Its columns are <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">gdp85<\/span>, <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">gdp60<\/span>, <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">invest<\/span>, <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">school<\/span>, <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">randd<\/span>, and <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">popgrowth<\/span>.<\/p>\n<p>To read the same file from your computer, use <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">T = rdata2table(&#8220;OECDGrowth.rda&#8221;)<\/span>. Use <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">readrdata<\/span> to import a workspace containing several named objects.<\/p>\n<h2 style=\"color: #c04f15; font-size: 22px; line-height: 1.3; margin: 32px 0 12px;\">Estimate a Model in R<\/h2>\n<p><a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/github.com\/cran\/AER\/blob\/master\/man\/USMacroSW.Rd\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">USMacroSW<\/span><\/a>, also from AER, contains quarterly data from 1957 Q1 to 2005 Q1. The dataset accompanies Stock and Watson\u2019s <i>Introduction to Econometrics<\/i>.<\/p>\n<p>Use the unemployment rate to fit a first-order autoregression:<\/p>\n<p style=\"margin: 12px 0 20px;\"><i>u<sub>t<\/sub> = a + \u03d5u<sub>t\u22121<\/sub> + \u03f5<sub>t<\/sub>.<\/i><\/p>\n<p>Here <i>t<\/i> indexes quarters; <i>u<sub>t<\/sub><\/i> is the unemployment rate in percent; and <i>u<sub>t\u22121<\/sub><\/i> is its value in the previous quarter. The intercept <i>a<\/i> is measured in percentage points, <i>\u03d5<\/i> is a dimensionless lag coefficient, and <i>\u03f5<sub>t<\/sub><\/i> is the one-quarter innovation, also in percentage points, assumed to have conditional mean zero.<\/p>\n<p>The estimation sample ends in 1995 Q4. Forecasts cover 1996 Q1 to 2005 Q1, each using the previous quarter\u2019s observed unemployment rate. The coefficients stay fixed.<\/p>\n<p>Create a data frame with unemployment, its lag, and a training flag. Use the first day of each quarter as its date label.<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\">url <span style=\"color: #8f5902;\">&lt;-<\/span> <span style=\"color: #204a87;\"><b>paste0<\/b><\/span>(\r\n    <span style=\"color: #4e9a06;\">\"https:\/\/raw.githubusercontent.com\/cran\/AER\/\"<\/span>,\r\n    <span style=\"color: #4e9a06;\">\"342a1842306047a279e47026c3800718221a3b2c\/data\/USMacroSW.rda\"<\/span>\r\n)\r\n<span style=\"color: #204a87;\"><b>download.file<\/b><\/span>(url, <span style=\"color: #4e9a06;\">\"USMacroSW.rda\"<\/span>, <span style=\"color: #204a87;\">mode =<\/span> <span style=\"color: #4e9a06;\">\"wb\"<\/span>)\r\n<span style=\"color: #204a87;\"><b>load<\/b><\/span>(<span style=\"color: #4e9a06;\">\"USMacroSW.rda\"<\/span>)\r\n\r\ndate <span style=\"color: #8f5902;\">&lt;-<\/span> <span style=\"color: #204a87;\"><b>seq<\/b><\/span>(<span style=\"color: #204a87;\"><b>as.Date<\/b><\/span>(<span style=\"color: #4e9a06;\">\"1957-01-01\"<\/span>), <span style=\"color: #204a87;\">by =<\/span> <span style=\"color: #4e9a06;\">\"quarter\"<\/span>,\r\n            <span style=\"color: #204a87;\">length.out =<\/span> <span style=\"color: #204a87;\"><b>nrow<\/b><\/span>(USMacroSW))\r\nu <span style=\"color: #8f5902;\">&lt;-<\/span> <span style=\"color: #204a87;\"><b>as.numeric<\/b><\/span>(USMacroSW[, <span style=\"color: #4e9a06;\">\"unemp\"<\/span>])\r\nmodel_data <span style=\"color: #8f5902;\">&lt;-<\/span> <span style=\"color: #204a87;\"><b>data.frame<\/b><\/span>(\r\n    <span style=\"color: #204a87;\">Date =<\/span> date[<span style=\"color: #ce5c00;\"><b>-<\/b><\/span><span style=\"color: #0000cf;\">1<\/span>],\r\n    <span style=\"color: #204a87;\">Unemployment =<\/span> u[<span style=\"color: #ce5c00;\"><b>-<\/b><\/span><span style=\"color: #0000cf;\">1<\/span>],\r\n    <span style=\"color: #204a87;\">LagUnemployment =<\/span> <span style=\"color: #204a87;\"><b>head<\/b><\/span>(u, <span style=\"color: #ce5c00;\"><b>-<\/b><\/span><span style=\"color: #0000cf;\">1<\/span>)\r\n)\r\nmodel_data<span style=\"color: #ce5c00;\"><b>$<\/b><\/span>Training <span style=\"color: #8f5902;\">&lt;-<\/span> model_data<span style=\"color: #ce5c00;\"><b>$<\/b><\/span>Date <span style=\"color: #ce5c00;\"><b>&lt;<\/b><\/span> <span style=\"color: #204a87;\"><b>as.Date<\/b><\/span>(<span style=\"color: #4e9a06;\">\"1996-01-01\"<\/span>)<\/pre>\n<p>Fit the model by ordinary least squares and save the data and coefficients:<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\">fit <span style=\"color: #8f5902;\">&lt;-<\/span> <span style=\"color: #204a87;\"><b>lm<\/b><\/span>(Unemployment <span style=\"color: #ce5c00;\"><b>~<\/b><\/span> LagUnemployment,\r\n          <span style=\"color: #204a87;\">data =<\/span> model_data, <span style=\"color: #204a87;\">subset =<\/span> Training)\r\nmodel_data<span style=\"color: #ce5c00;\"><b>$<\/b><\/span>RForecast <span style=\"color: #8f5902;\">&lt;-<\/span> <span style=\"color: #204a87;\"><b>as.numeric<\/b><\/span>(<span style=\"color: #204a87;\"><b>predict<\/b><\/span>(fit, <span style=\"color: #204a87;\">newdata =<\/span> model_data))\r\ncoefficients <span style=\"color: #8f5902;\">&lt;-<\/span> <span style=\"color: #204a87;\"><b>data.frame<\/b><\/span>(\r\n    <span style=\"color: #204a87;\">Term =<\/span> <span style=\"color: #204a87;\"><b>c<\/b><\/span>(<span style=\"color: #4e9a06;\">\"Intercept\"<\/span>, <span style=\"color: #4e9a06;\">\"LagUnemployment\"<\/span>),\r\n    <span style=\"color: #204a87;\">Estimate =<\/span> <span style=\"color: #204a87;\"><b>unname<\/b><\/span>(<span style=\"color: #204a87;\"><b>coef<\/b><\/span>(fit))\r\n)\r\n\r\n<span style=\"color: #204a87;\"><b>saveRDS<\/b><\/span>(model_data, <span style=\"color: #4e9a06;\">\"unemployment.rds\"<\/span>, <span style=\"color: #204a87;\">compress =<\/span> <span style=\"color: #4e9a06;\">\"gzip\"<\/span>, <span style=\"color: #204a87;\">version =<\/span> <span style=\"color: #0000cf;\">3<\/span>)\r\n<span style=\"color: #204a87;\"><b>save<\/b><\/span>(coefficients, <span style=\"color: #204a87;\">file =<\/span> <span style=\"color: #4e9a06;\">\"baseline.RData\"<\/span>,\r\n     <span style=\"color: #204a87;\">compress =<\/span> <span style=\"color: #4e9a06;\">\"gzip\"<\/span>, <span style=\"color: #204a87;\">version =<\/span> <span style=\"color: #0000cf;\">3<\/span>)<\/pre>\n<p>The files contain the dated observations, training flags, reference forecasts, and estimated coefficients. MATLAB will use these saved values in the next step.<\/p>\n<h2 style=\"color: #c04f15; font-size: 22px; line-height: 1.3; margin: 32px 0 12px;\">Import the R Results into MATLAB<\/h2>\n<p>In MATLAB, put <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">unemployment.rds<\/span> and <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">baseline.RData<\/span> in the current folder and run:<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\"><span style=\"color: #000000;\">TT<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">rdata2table<\/span>(<span style=\"color: #4e9a06;\">\"unemployment.rds\"<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #ce5c00;\"><b>...<\/b><\/span>\r\n    <span style=\"color: #000000;\">OutputType<\/span><span style=\"color: #ce5c00;\"><b>=<\/b><\/span><span style=\"color: #4e9a06;\">\"timetable\"<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #ce5c00;\"><b>...<\/b><\/span>\r\n    <span style=\"color: #000000;\">RowTimes<\/span><span style=\"color: #ce5c00;\"><b>=<\/b><\/span><span style=\"color: #4e9a06;\">\"Date\"<\/span>)<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n\r\n<span style=\"color: #000000;\">baseline<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">readrdata<\/span>(<span style=\"color: #4e9a06;\">\"baseline.RData\"<\/span>)<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">coefficients<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">baseline<\/span>{<span style=\"color: #4e9a06;\">\"coefficients\"<\/span>}<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">head<\/span>(<span style=\"color: #000000;\">TT<\/span>)<\/pre>\n<p>The <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">Date<\/span> column becomes the timetable\u2019s row times. The other columns contain unemployment, its lag, the training flag, and the R forecast. Creating the lag leaves 192 observations: 155 for estimation and 37 for evaluation.<\/p>\n<figure style=\"margin: 20px 0;\"><img decoding=\"async\" style=\"height: auto; max-width: 100%;\" src=\"http:\/\/blogs.mathworks.com\/finance\/files\/2026\/11\/imported-data-table.png\" alt=\"The first eight quarters of the unemployment timetable imported from R, with date, unemployment, lag, training, and forecast columns.\" \/><figcaption style=\"font-style: italic; margin-top: 8px;\">First eight rows imported from R, displayed in a MATLAB table. Numeric values are shown to four decimal places.<\/figcaption><\/figure>\n<p><span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">readrdata<\/span> returns a dictionary, with each saved R object available by its original name. <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">baseline{&#8220;coefficients&#8221;}<\/span> retrieves the coefficient table. Named R lists also become dictionaries.<\/p>\n<p>You can also inspect the imported column types and missing-value metadata:<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\"><span style=\"color: #000000;\">variableMetadata<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">Properties<\/span>.<span style=\"color: #000000;\">CustomProperties<\/span>.<span style=\"color: #000000;\">RDataVariableMetadata<\/span><span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">timeMetadata<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">Properties<\/span>.<span style=\"color: #000000;\">CustomProperties<\/span>.<span style=\"color: #000000;\">RDataRowTimeMetadata<\/span><span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">variableMetadata<\/span>(<span style=\"color: #ce5c00;\"><b>:,<\/b><\/span> [<span style=\"color: #4e9a06;\">\"VariableName\"<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #4e9a06;\">\"RType\"<\/span><span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #4e9a06;\">\"MissingMask\"<\/span>])<\/pre>\n<p>Use <span style=\"font-family: Consolas, 'Courier New', monospace; background-color: #f0f0f0;\">MissingMask<\/span> to identify missing values in each variable. The <a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/github.com\/mathworks\/matlab-reader-for-r-data-files\/blob\/main\/tbx\/doc\/readrds.md\" target=\"_blank\" rel=\"noopener noreferrer\">reader documentation<\/a> explains the metadata fields.<\/p>\n<h2 style=\"color: #c04f15; font-size: 22px; line-height: 1.3; margin: 32px 0 12px;\">Compare the Forecasts<\/h2>\n<p>Use the imported coefficients to calculate the same one-quarter-ahead forecasts:<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\"><span style=\"color: #000000;\">b<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">coefficients<\/span>.<span style=\"color: #000000;\">Estimate<\/span><span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">matlabForecast<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">b<\/span>(<span style=\"color: #0000cf;\">1<\/span>) <span style=\"color: #ce5c00;\"><b>+<\/b><\/span> <span style=\"color: #000000;\">b<\/span>(<span style=\"color: #0000cf;\">2<\/span>)<span style=\"color: #ce5c00;\"><b>*<\/b><\/span><span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">LagUnemployment<\/span><span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">evaluation<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #ce5c00;\"><b>~<\/b><\/span><span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">Training<\/span><span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n\r\n<span style=\"color: #000000;\">maxDifference<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">max<\/span>(<span style=\"color: #000000;\">abs<\/span>( <span style=\"color: #ce5c00;\"><b>...<\/b><\/span>\r\n    <span style=\"color: #000000;\">matlabForecast<\/span>(<span style=\"color: #000000;\">evaluation<\/span>) <span style=\"color: #ce5c00;\"><b>-<\/b><\/span> <span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">RForecast<\/span>(<span style=\"color: #000000;\">evaluation<\/span>)))<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">assert<\/span>(<span style=\"color: #000000;\">maxDifference<\/span> <span style=\"color: #ce5c00;\"><b>&lt;<\/b><\/span> <span style=\"color: #0000cf;\">1e-12<\/span>)<\/pre>\n<p>The 37 forecasts calculated in MATLAB match those saved by R.<\/p>\n<figure style=\"margin: 20px 0;\"><img decoding=\"async\" style=\"height: auto; max-width: 100%;\" src=\"http:\/\/blogs.mathworks.com\/finance\/files\/2026\/11\/forecast-comparison.png\" alt=\"Observed unemployment and the R and MATLAB forecasts from 1996 to 2005, with a lower panel showing the forecast difference.\" width=\"1381\" \/><figcaption style=\"font-style: italic; margin-top: 8px;\">R forecasts (blue line) and forecasts calculated in MATLAB (green circles). The lower panel shows the difference. Data: AER USMacroSW; Stock and Watson (2007).<\/figcaption><\/figure>\n<p>You can also estimate the coefficients directly in MATLAB:<\/p>\n<pre style=\"background-color: #f0f0f0; border: 0; box-shadow: none; color: #222; font-family: Consolas, 'Courier New', monospace; font-size: 13px; line-height: 1.45; margin: 12px 0 20px; padding: 12px 16px; white-space: pre;\"><span style=\"color: #000000;\">X<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> [<span style=\"color: #000000;\">ones<\/span>(<span style=\"color: #000000;\">height<\/span>(<span style=\"color: #000000;\">TT<\/span>)<span style=\"color: #ce5c00;\"><b>,<\/b><\/span><span style=\"color: #0000cf;\">1<\/span>)<span style=\"color: #ce5c00;\"><b>,<\/b><\/span> <span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">LagUnemployment<\/span>]<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">bMatlab<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">X<\/span>(<span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">Training<\/span><span style=\"color: #ce5c00;\"><b>,:<\/b><\/span>) <span style=\"color: #ce5c00;\"><b>\\<\/b><\/span> <span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">Unemployment<\/span>(<span style=\"color: #000000;\">TT<\/span>.<span style=\"color: #000000;\">Training<\/span>)<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">maxCoefficientDifference<\/span> <span style=\"color: #ce5c00;\"><b>=<\/b><\/span> <span style=\"color: #000000;\">max<\/span>(<span style=\"color: #000000;\">abs<\/span>(<span style=\"color: #000000;\">bMatlab<\/span> <span style=\"color: #ce5c00;\"><b>-<\/b><\/span> <span style=\"color: #000000;\">b<\/span>))<span style=\"color: #ce5c00;\"><b>;<\/b><\/span>\r\n<span style=\"color: #000000;\">assert<\/span>(<span style=\"color: #000000;\">maxCoefficientDifference<\/span> <span style=\"color: #ce5c00;\"><b>&lt;<\/b><\/span> <span style=\"color: #0000cf;\">1e-12<\/span>)<\/pre>\n<p>The coefficients estimated in MATLAB match those from R.<\/p>\n<h2 style=\"color: #c04f15; font-size: 22px; line-height: 1.3; margin: 32px 0 12px;\">Continue the Analysis in MATLAB<\/h2>\n<p>You can use the same approach to pass estimates from R into an existing MATLAB forecasting model or reporting script. The files could hold coefficients, posterior draws, or forecast series from your own R analysis.<\/p>\n<p>Saved R results also give you a reference when re-estimating a model in MATLAB. For other time series models, <a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/www.mathworks.com\/products\/econometrics.html\" target=\"_blank\" rel=\"noopener noreferrer\">Econometrics Toolbox<\/a> provides estimation, forecasting, and diagnostic tools.<\/p>\n<p>Try the <a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/github.com\/mathworks\/matlab-reader-for-r-data-files\/blob\/main\/tbx\/doc\/getting-started.md\" target=\"_blank\" rel=\"noopener noreferrer\">Getting Started guide<\/a> or download the <a style=\"color: #2f7eb2; text-decoration: underline;\" href=\"https:\/\/github.com\/mathworks\/matlab-reader-for-r-data-files\/archive\/refs\/heads\/main.zip\" target=\"_blank\" rel=\"noopener noreferrer\">R and MATLAB example<\/a> to run the code from this post.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"http:\/\/blogs.mathworks.com\/finance\/files\/2026\/11\/imported-data-table.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>\nIf you estimate models in R and use MATLAB for forecasting or reporting, you can import the saved data and results directly.<br \/>\nMATLAB Reader for R Data Files reads .rds, .rda, and .RData files. It&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/finance\/2026\/11\/23\/bring-r-data-and-model-results-into-matlab\/\">read more >><\/a><\/p>\n","protected":false},"author":205,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,16,37,34],"tags":[],"class_list":["post-3096","post","type-post","status-publish","format-standard","hentry","category-data-management","category-econometrics","category-forecasting","category-time-series"],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts\/3096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/users\/205"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/comments?post=3096"}],"version-history":[{"count":15,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts\/3096\/revisions"}],"predecessor-version":[{"id":3107,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/posts\/3096\/revisions\/3107"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/media?parent=3096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/categories?post=3096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/finance\/wp-json\/wp\/v2\/tags?post=3096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}