41108

How to get constant term in AR Model with statsmodels and Python?

I'm trying to model my time series data using the AR model.

<img src="https://i.stack.imgur.com/U63s7.png" alt="enter image description here">

This is the code that I'm using.

# Compute AR-model (data is a python list of number) model = AR(data) result = model.fit() plt.plot(data, 'b-', label='data') plt.plot(range(result.k_ar, len(data)), result.fittedvalues, 'r-') plt.show()

I've successfully get the p value using result.k_ar, parameter with result.params, epsilon term with result.sigma2. The problem is that I can't find a way to get the c (constant) term. Here is the code I write to compare the result.

# Plot fit = [] for t in range(result.k_ar, len(data)): value = 0 for i in range(1, result.k_ar+1): value += result.params[i-1] * data[t - i] fit.append(value) plt.plot(data, 'b-', label='data') plt.plot(range(result.k_ar, len(data)), fit, 'r-', label='fit') plt.plot(range(result.k_ar, len(data)), result.fittedvalues, 'r-') plt.show()

My result and the result from result.fittedvalues confirm my evident that there is some constant term added to the model. Thanks.

<img src="https://i.stack.imgur.com/VekrS.png" alt="enter image description here">

Answer1:

The constant is the zero-th element in params. E.g., params[0].

Your code should be

fit = [] for t in range(result.k_ar, len(data)): value = result.params[0] for i in range(2, result.k_ar + 2): value += result.params[i - 1] * data[t - i + 1] fit.append(value)

Or even easier, since we've made the lag matrix for you (this is what fittedvalues does)

np.dot(result.model.X, result.params)

As an aside, note that for AR this is actually the constant and not the mean. The mean is reported by the ARMA model, which is a bit more full-featured than the plain AR model. (It has a summary method that reports the constant. AR should too but doesn't.) The connection is

constant = mean(1 - arparams.sum())

Recommend

  • matlab efficient way to extract diagonals of a 3D matrix [duplicate]
  • Cassandra: What is a subcolumn
  • How can I count unique terms in a plaintext file case-insensitively?
  • Programmatically Update Linked Named Range of excel object in MS Word (2007)
  • Image map in Flex
  • xcode don't localize specific strings
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Using JRuby with Rails 3.2
  • Excel's Macro-Recorder usage
  • Remove final comma from string in vb.net
  • onBackPressed() not being executed
  • How can I sort a a table with VBA with given text condition?
  • Swift: Switch statement fallthrough behavior
  • copying resource to sdcard gives a damaged file in android
  • Allowing both email and username for authentication
  • Python CGI os.system causing malformed header
  • Is there a javascript serializer for JSON.Net?
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Nant, Vault & Windows Integrated Authentication
  • Regex thinks I'm nesting, but I'm not
  • Fetching methods from BroadcastReceiver to update UI
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Bug in WPF DataGrid
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • Sending data from AppleScript to FileMaker records
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • How to model a transition system with SPIN
  • VBA Convert delimiter text file to Excel
  • ORA-29908: missing primary invocation for ancillary operator
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Reading document lines to the user (python)
  • Converting MP3 duration time