How do you count the number of parameters in a time series model? I am learning about time series analysis and want to perform Box-Ljung tests of the residuals of my fitted models, e.g. 
Box.test(res, lag=h, fitdf=K, type = "Lj") 

However, I cannot find a way to calculate $K$. The only heuristics I have thus far are:


*

*$K$ is equal to the number of parameters in the model.

*If the residuals are calculated on raw data, $K = 0$.

*If the model has no parameters, $K = 0$.


I found a formula for the number of parameters in a non-seasonal ARIMA model for a time series, stating $n = p + q + k + 1$, where $k$ is an indicator function for the constant $c$, that is, $c = k = 0$ if $d \ge 1$, but in the examples I have found, this formula doesn't agree with the $K$ used in the Box-Ljung test.
Can anybody help me count the parameters?
 A: So, assuming you use ARIMA, the following applies:
1) when you have a model without differencing i.e. $ARIMA(p,d=0,q)$, then $K=p+q+1$, the $1$ comes from the model constant.
2) when you have a model $ARIMA(p,d>0,q)$, then $K=p+q$
You could look at prof. Hyndman's online free textbook
A: The help file for Box.test function in R states These tests are sometimes applied to the residuals from an ARMA(p, q) fit, in which case the references suggest a better approximation to the null-hypothesis distribution is obtained by setting fitdf = p+q, provided of course that lag > fitdf. Thus the suggestion is to use $K=p+q$ for an ARMA(p,q) model. 
Conceptually, the key question is what the null distribution of the test statistic is given the model specification (and the implications on model residuals which are supplied for the Ljung-Box test). Note that the effective degrees of freedom to be used in a particular test depends on the test, not only on the model that generated the residuals. That is why, for example, you are not counting the estimated variance when you think of Ljung-Box test even though variance is a parameter of the model. Another example: in testing for remaining ARCH effects in GARCH model residuals using ARCH-LM test you discard the count of ARMA parameters but do count the GARCH parameters.
Hyndman and Athana­sopou­los textbook chapter about Ljung-Box test can be found here.
