最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

statistics - R - Replacement Not Multiple Length Error for Zoo Object - Stack Overflow

programmeradmin3浏览0评论

I am trying to implement the VAR-based ConnectednessApproach R package, which requires a zoo object as an input. I have separate data frames that I merge together with an inner_join, which yields some items with NA, Inf, -Inf. I first tried removing all the rows with these errors by changing all the Inf, -Inf into NAs, and then using na.omit().

However, that failed to resolve the error, so I then tried substituting the NAs with the mean of the columns. I then again run the diagnostics and see that after convering to zoo, there is some sort of inconsistency in the index, but I cannot understand what is wrong with it.

#Check for Invalid Data Types in the combined Data Frame
colSums(is.na(GPRD_combined))   
colSums(GPRD_combined == Inf, na.rm = TRUE) 
colSums(GPRD_combined == -Inf, na.rm = TRUE) 

#Replace Infinites with NA and Remove all NAs to clean
GPRD_combined[GPRD_combined == Inf | GPRD_combined == -Inf] <- NA
GPRD_combined <- na.omit(GPRD_combined)

#Alternate - Replace NAs with Mean
for(i in 1:ncol(GPRD_combined)) {
  column_mean <- mean(GPRD_combined[[i]], na.rm = TRUE)
  GPRD_combined[is.infinite(GPRD_combined[, i]) | is.na(GPRD_combined[, i]), i] <- column_mean
}

#Alternate - Replace NAs with Last Observation Carried Forward
GPRD_combined_filled <- zoo::na.locf(GPRD_combined, na.rm = FALSE)

#Analyze the Consistency of the Zoo Series
sum(is.na(GPRD_combined))  # Check for NA values
all.equal(index(GPRD_combined), index(GPRD_combined)[1]) #Check Time series consistency
length(unique(index(GPRD_combined)))
sum(duplicated(index(GPRD_combined)))
str(index(GPRD_combined))
GPRD_combined$newdate <- as.Date(GPRD_combined$newdate)

#Convert Date Frame into Zoo for ConnectednessApproach
GPRD_combined <- zoo(GPRD_combined[,-1], order.by = as.Date(GPRD_combined$newdate))
print(GPRD_combined)

#Static Connectedness
CA_test = ConnectednessApproach(GPRD_combined,
                                nlag=4,nfore=10,
                                model="VAR", connectedness = "Time")

fit = VAR(GPRD_combined, configuration=list(nlag=4))
GPRD_combined = TimeConnectedness(fit$B, fit$Q, nfore=10)

When I substitute the errors with the mean and then attempt to create a zoo object for the ConnectednessApproach function, I get the following results from the console

> GPRD_combined[GPRD_combined == Inf | GPRD_combined == -Inf] <- NA
> for(i in 1:ncol(GPRD_combined)) {
+   column_mean <- mean(GPRD_combined[[i]], na.rm = TRUE)
+   GPRD_combined[is.infinite(GPRD_combined[, i]) | is.na(GPRD_combined[, i]), i] <- column_mean
+ }
> sum(is.na(GPRD_combined))  # Check for NA values
[1] 0
> all.equal(index(GPRD_combined), index(GPRD_combined)[1]) #Check Time series consistency
[1] "Numeric: lengths (5114, 1) differ"
> length(unique(index(GPRD_combined)))
[1] 5114
> sum(duplicated(index(GPRD_combined)))
[1] 0
> str(index(GPRD_combined))
 int [1:5114] 1 2 3 4 5 6 7 8 9 10 ...
> GPRD_combined$newdate <- as.Date(GPRD_combined$newdate)
> GPRD_combined <- zoo(GPRD_combined[,-1], order.by = as.Date(GPRD_combined$newdate))
> CA_test = ConnectednessApproach(GPRD_combined,
+                                 nlag=4,nfore=10,
+                                 model="VAR", connectedness = "Time")
Estimating model
Error in B_t[, , i] <- fit$B : 
  number of items to replace is not a multiple of replacement length

Oddly enough, when I attempt to implement a VAR model into the Time Connectedness Index by Diebold & Yilmaz (2014), it appears to work

> GPRD_combined = TimeConnectedness(fit$B, fit$Q, nfore=10)
> kable(fit$Q)


|                      | GPR_raw.retGPRD.2024-12-31.....

I must be able to use the ConnectednessApproach package for a dynamic connectedness analysis, so I cannot take a manual approach as I just did.

发布评论

评论列表(0)

  1. 暂无评论