1
$\begingroup$

I am currently working on a project to determine how strongly the sample size determining one time series affects another (fixed) one.

rsquaredjpn[s_] := LinearModelFit[Partition[Flatten[Riffle[regressionlistjpn[s], jpngdp]], 2], x, x]["RSquared"]
plotjpn = ListLinePlot[Table[{s, rsquaredjpn[t]}, {s, 1, 100}], PlotRange -> All, AxesLabel -> {"K", "\!\(\*SuperscriptBox[\(R\), \(2\)]\)"}]

The list "jpngdp" as the dependent variables consists of 35 fixed values, while "regressionlistjpn[s]" delivers 35 values depending on s, the sample size. The end result is the plot "plotjpn", where K denotes the sample size on the x-axis and R^2 denotes the resulting R^2 values for every individual regression based on the sample size that generated regressionlistjpn[s] ranging from a size of 1 to 100.

Now, I would like to included lagged terms in the regression such that the specification states that jpngdp at t is not only dependent on the value of regressionlistjpn at t, but also at t-1 (and maybe t-2, ...). Here in the forum as well as in the Mathematica documentation, I could not find anything to help me with this.

So, my question would be how to specify above linear regression to also include lagged terms as the independent variables.

Help would be greatly appreciated!

$\endgroup$
2
$\begingroup$

You don't need to do Partition[Flatten[Riffle[regressionlistjpn[s], jpngdp]], 2]; simply doing Transpose[{regressionlistjpn[s], jpngdp}] will do the trick, as long as what you're trying to achieve is 35 pairs where the last entry in each pair is taken from jpngdp.

In lieu of actual data, I simulate the relevant numbers in BlockRandom; I assume your data come from a database with dimensions {100,35} (see data in the code below); I also assume that jpngdp is a constant (random real) vector of 35 entries (see below).

With[{n = 100, yMn = 127 37 10^3, yMx = 127 50 10^3, s = 35},

  Module[{data, x},

    BlockRandom[
      data = RandomReal[{yMn, yMx}, {n, s}];

      jpngdp = RandomReal[{yMn, yMx}, s];, RandomSeeding -> 123456789];

    regressionlistjpn[n_] := Part[data, n];

    rsquaredjpn[s_] := LinearModelFit[
      Transpose[{regressionlistjpn[s], jpngdp}], {1, x}, x]["RSquared"];

    plotjpn = ListLinePlot[
      Table[{i, rsquaredjpn[i]}, {i, 1, 100}],
      PlotRange -> All,
      AxesLabel -> {"K", "R^2"}
     ]

   ]

 ]

I hope everything else is pretty straightforward. The output I get on the simulated data is

enter image description here

As far as incorporating lags in the specification, you can use

With[{n = 100, yMn = 127 37 10^3, yMx = 127 50 10^3, s = 35, l = 1},

  Module[{data, x, xs, y, syms},

    BlockRandom[

      data = RandomReal[{yMn, yMx}, {n, s}];

      jpngdp = RandomReal[{yMn, yMx}, s];, RandomSeeding -> 123456789];

    regressionlistjpn[n_] := Part[data, n];

    xs[s_, lag_] := Table[
      Drop[Drop[regressionlistjpn[s], -i], lag - i], {i, 0, lag}];

    y[lag_] := Drop[jpngdp, lag];

    syms[lag_] := Table[
       ToExpression[StringJoin[{"LAG", ToString[i]}]], {i, 0, lag}];

    rsquaredjpn[s_, lag_] := LinearModelFit[
      Transpose[Flatten[{xs[s, lag], {y[lag]}}, 1]], 
        syms[lag], syms[lag], IncludeConstantBasis -> True]["RSquared"];

    plotjpn = ListLinePlot[
       Table[{i, rsquaredjpn[i, l]}, {i, 1, 100}],
       PlotRange -> All,
       AxesLabel -> {"K", "R^2"}]

  ]

]

The output I get on the simulated data for one lag is

enter image description here

$\endgroup$
  • $\begingroup$ Thank you so much, everything is working now! $\endgroup$ – Jan Schulz Dec 15 '17 at 12:41
  • $\begingroup$ please make sure your input data are appropriately structured (so as to make sense to evaluate something like Transpose[{regressionlistjpn[s], jpngdp}]) otherwise, you might be getting garbage as output even if Mathematica doesn't issue any warnings; also, you might be interested in TimeSeries $\endgroup$ – user42582 Dec 15 '17 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.