1
votes

I am a newbie in R and I am learning how to apply my own function to matrices. I wrote the following code:

HAR_ReV<-function(vec){
  name<-rlang::enexpr(vec)
  x<-vec
  ReV_d = ReV_w = ReV_m = NULL

  TT = length(x)
  for (i in 31:TT){
    ReV_d[i] = x[(i-1)]
    ReV_w[i] = sum(x[(i-7):(i-1)],na.rm = T)/7 
    ReV_m[i] = sum(x[(i-30):(i-1)],na.rm = T)/30 
  }
  Realized_Variances = cbind(ReV_d, ReV_w, ReV_m)
  colnames(Realized_Variances)= c(paste("ReV_d",name, sep = "_"), 
                                  paste("ReV_w",name, sep = "_"),
                                  paste("ReV_m",name, sep = "_"))
  Realized_Variances
}

The code was made for being applied in vectors and the function above create a new matrix, size 3530 (including NA's) X 3, for each vector. Now, I have a matrix that contains all vectors and I want to apply this function in order to obtain a single matrix size 3530 x 36.

My Matrix has the following structure:

Matrix<-matrix(1:45, nrow = 3530,   ncol = 12)
dimnames(Matrix) <- list(NULL, NULL)
dimnames(Matrix) <- list(c(1:3530), c("rrp_nsw_d", "rrp_qld_d"  , "rrp_sa_d", "rrp_vic_d",     "rrp_nsw_RV_pos", "rrp_qld_RV_pos", "rrp_sa_RV_pos", "rrp_vic_RV_pos", 
                      "rrp_nsw_RV_neg" , "rrp_qld_RV_neg", "rrp_sa_RV_neg", "rrp_vic_RV_neg"))

Thanks for your help

2
Please show how to call this function with one vector that does not yield an error.Parfait
@Parfait , for a single vector this would be the code example<-as.numeric(c(1:3530)) and the function would be HAR_ReV(example)Eve Chanatasig

2 Answers

1
votes

Consider lapply to build a list of matrices that you can then cbind together at the end. However, to do so, a slight adjustment is needed. Instead of defining name with rlang::enexpr that takes literal argument name, consider passing name as a parameter such as the original matrix column number. Also, define your vectors length in advance instead of growing them in a loop:

Adjusted Function

HAR_ReV <- function(vec, name){
  #name <- rlang::enexpr(vec)          # REMOVE LINE
  x <- vec
  TT <- length(x)
  # DEFINE VECTOR LENGTH IN ADVANCE (MORE EFFICIENT THAN GROWING THEM IN LOOP)
  ReV_d <- ReV_w <- ReV_m <- vector(mode="numeric", length=TT-30)

  for (i in 31:TT){
    ReV_d[i] <- x[(i-1)]
    ReV_w[i] <- sum(x[(i-7):(i-1)],na.rm = T)/7 
    ReV_m[i] <- sum(x[(i-30):(i-1)],na.rm = T)/30 
  }
  Realized_Variances <- cbind(ReV_d, ReV_w, ReV_m)
  colnames(Realized_Variances) <- c(paste("ReV_d", name, sep = "_"), 
                                    paste("ReV_w", name, sep = "_"),
                                    paste("ReV_m", name, sep = "_"))
  return(Realized_Variances)
}

Matrix Build

my_matrix <- matrix(1:45, nrow = 3530, ncol = 12,
                    dimnames = list(c(1:3530), 
                                    c("rrp_nsw_d", "rrp_qld_d"  , "rrp_sa_d", "rrp_vic_d",     
                                      "rrp_nsw_RV_pos", "rrp_qld_RV_pos", "rrp_sa_RV_pos", 
                                      "rrp_vic_RV_pos", "rrp_nsw_RV_neg" , "rrp_qld_RV_neg", 
                                      "rrp_sa_RV_neg", "rrp_vic_RV_neg")
                    )
)

# LIST OF MATRICES
final_matrix_list <- Map(function(i,n) HAR_ReV(my_matrix[,i], n), 
                         1:ncol(my_matrix), colnames(my_matrix))

# FINAL MATRIX
final_matrix <- do.call(cbind, final_matrix_list)

dim(final_matrix)
# [1] 3530   36

str(final_matrix)
# num [1:3530, 1:36] 0 0 0 0 0 0 0 0 0 0 ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:36] "ReV_d_rrp_nsw_d" "ReV_w_rrp_nsw_d" "ReV_m_rrp_nsw_d" "ReV_d_rrp_qld_d" ...

str(final_matrix)
1
votes

The following will give you the result you are looking for:

result <- as.matrix(do.call("cbind", lapply(as.data.frame(Matrix), 
                                            function(x) {
                                              y <- as.integer(x); 
                                              HAR_ReV(y)
                                            })))

Obviously I can't fit the result in here, but you can check out the dimensions of the result:

dim(result)
#> [1] 3530   36