Hi everyone,

So I just installed the "astsa" package in R studio. When I put the command" acf2 (dataname)", It shows that "Error in acf2(dataname): number of lags exceeds number of observations". Does anyone know how to fix it? Many thanks!

# Unable to show ACF graph

**claradeng**#1

**andresrcs**#2

We don't really have enough info to help you out. Could you ask this with a minimal **REPR**oducible **EX**ample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

**Yarnabrina**#3

This error is very much self-explanatory.

What do you do in ACF and PACF? You try to find the correlation between the observations with their lagged versions in ACF, and in PACF you do the same with removing the effect of the versions with shorter lags.

So, suppose you have data points x_1, x_2, \dots, x_n.

Then, for lag 1, you find the correlation between (x_1, x_2, \dots, x_{n - 1}) and (x_2, x_3, \dots, x_n), for lag 2, you find the correlation between (x_1, x_2, \dots, x_{n - 2}) and (x_3, x_4, \dots, x_n), and so on.

Note that the number of available pairs of observations and their lagged versions decrease as lag increases, and at lag k, there are n - k pairs. So, if you use higher values of lag than the number of observations (strictly speaking, you can use lag of at most one less than the number of observations), then there's no data available to calculate the correlation and hence you're getting errors.

So, try to reduce the number of lags, and you'll get the desired results.

Hope this helps.

**claradeng**#4

So my dataset has 360 observations with two variables (one being time and one being rate). I reduced the number of lags by putting the command "acf2(dataname, max.lag=10", but it still shows "number of lags exceeds the number of observations." Then I found that it kept giving me that same feedback up till I reduced the max.lag to 1, when the feedback changed into "error in xy.coords(x,y,xlable, ylabel,log):'x' and 'y' lengths differ. What could be a possible reason for this?

**Yarnabrina**#5

As Andres has advised earlier, a REPRoducible EXample will be very helpful. It helps others to help you. Please go through the link he shared above to make a reprex.

Without any further information, I'll guess that `acf2`

considers the rows of the `data.frame`

as series and hence it's guessing that they are of length 2. So, you're allowed to use 1 as the maximum lag.

If you want to calculate ACF and PACF for both columns of your dataset, try to use `lapply`

. See below:

```
library(astsa)
u <- rnorm(n = 360)
v <- rnorm(n = 360)
acf_u <- acf2(series = u)
```

```
acf_v <- acf2(series = v)
```

```
df <- data.frame(u, v)
acf_df <- lapply(X = df,
FUN = acf2)
```

```
all.equal(target = list(u = acf_u, v = acf_v),
current = acf_df)
#> [1] TRUE
```

^{Created on 2019-04-18 by the reprex package (v0.2.1)}

**system**closed #6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.