TYL
TYL

Reputation: 1637

Error when using ave for custom function in r

I am using the ave() function to find the partial autocorrelation (pacf) for each individual subject in my data.

# return pacf coefficient
pacf1 = function(x) { 
  return(pacf(x, lag.max = 1, na.action=na.pass)$acf[1])
}

ave(df$score, df$id, FUN=pacf1) # get pacf coefficient for each ID

Error in ts(x) : 'ts' object must have one or more observations

I keep getting the time series error on my original huge dataframe. However, when I did it on a sample "made-up" dataframe, this code works. I am not sure why this is an issue.

Is there another way of getting my desired output without using the ave function?

Thanks!

Upvotes: 1

Views: 195

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

As explained in the comments the issue is because some factor levels were filtered from the dataset resulting in an error while using ave. We can verify this using the default mtcars dataset

df <- mtcars 
df$cyl <- as.factor(df$cyl) #Convert cyl to factor
df <- subset(df, cyl!= 4)   #keep rows where cyl is not equal to 4
ave(df$mpg, df$cyl, FUN=pacf1)

Error in ts(x) : 'ts' object must have one or more observations

We can resolve this by dropping the unused levels

df$cyl <- droplevels(df$cyl)
ave(df$mpg, df$cyl, FUN=pacf1)

#[1] 0.234 0.234 0.234 0.209 0.234 0.209 0.234 0.234 0.209 0.209 0.209 0.209
#    0.209 0.209 0.209 0.209 0.209 0.209 0.209 0.234 0.209

#Or as @thelatemail mentions use 
#ave(df$mpg, df$cyl, FUN=pacf1, drop = TRUE)

if we use dplyr or data.table this issue is handled automatically.

library(dplyr)
df %>%
  group_by(cyl) %>%
  mutate(score = pacf1(mpg))


library(data.table)
setDT(df)[, score := pacf1(mpg), by = cyl]

Upvotes: 3

Related Questions