Negative or Zero Value for Bandwidth using kde2d with for loop (MASS R Package)

317 Views Asked by At

I am trying to run a two-dimensional kde using kde2d(MASS package) over multiple data frames within a list. When trying to run the loop, I get:

Error in if (any(h <= 0)) stop("bandwidths must be strictly positive") : missing value where TRUE/FALSE needed.

Does anyone know why I am getting this error? I've added sample data below as an example of what is being looped over.

RTL_EOW DSL_SLS Avg_Wkly_Tmp PROXY_ID
8/27/2016 0 78.65940641 1
9/3/2016 2924 68.28091649 1
9/10/2016 2656 69.3189759 1
9/17/2016 5050 71.61575243 1
9/24/2016 4254 73.85357591 1
10/1/2016 5316 61.63126437 1
10/8/2016 5582 57.10903173 1
10/15/2016 5531 66.50587123 3
10/22/2016 5046 53.08352004 3
10/29/2016 8195 68.44048727 3
11/5/2016 5535 52.66686763 3
11/12/2016 6863 41.69310232 3
11/19/2016 7976 38.96718858 3

K <- for (i in 1:length(Q)) {
Q[[i]] <- kde2d(Q[[i]]$Avg_Wkly_Tmp, 
Q[[i]]$Dsnl_Sls, n = 25, lims 
c(min(Q[[i]]$Avg_Wkly_Tmp), 
max(Q[[i]]$Avg_Wkly_Tmp), 
min(Q[[i]]$Dsnl_Sls), max(Q[[i]]$Dsnl_Sls)))
}
1

There are 1 best solutions below

1
On

We don't have Q, but it looks like at least one element of it has zero range.