Put Correlation Output of Function into Data Frame - r

I am performing a lot of correlations of a populations over time. I have split them up accordingly and have put them through a function with lapply. I want to put the output of each correlation into a data frame (i.e.: each row will be the info for one correlation, with the columns: correlation's name, p-value, t statistic, df, CIs, and corcoeff).
I have two issues:
I don't know how to extract the name of the correlation made in the split
I can get my function to run the correlation on the split (600+ Correlations), but I can't get it to print it into the data frame. To clarify: When I run the function without the loop, it does all 600 Correlations for each group. However, when I add the loop, it produces NULL for all the groups in the split.
Here is what I have thus far:
> head(Birds) #Shortened for this Post
Location Species Year Longitude Latitude Section Total Percent Family
1 Chiswell A Kittiwake 1976 -149.5847 59.59559 Central 310 16.78397 Gull
BigSplit<-split(Birds,list(Birds$Family, Birds$Location,
Birds$Section,Birds$Species), drop=T) #A list of Dataframes
#Make empty data frame
resultcor <- data.frame(Name = character(),
tvalue = character(),
degreeF = character(),
pvalue = character(),
CIs = character(),
corcoeff = character(),stringsAsFactors = F)
WorkFunc <- function(dataset) {
data.name = substitute(dataset) #Use "dataset" as substitute for actual dataset name
#Correlation between Year and population Percent
try({
correlation <- cor.test(dataset$Year, dataset$Percent, method = "pearson")
}, silent = TRUE)
for (i in 1:nrow(resultcor)) {
resultcor$Name[i] <- ??? #These ??? are not in the code, just highlighting Issue 1
resultcor$tvalue[i] <- correlation$dataset$statistic
resultcor$degreeF[i] <- correlation$dataset$parameter
resultcor$pvalue[i] <- correlation$dataset$p.value
resultcor$CIs[i] <- correlation$dataset$conf.int
resultcor$corcoeff[i] <- correlation$dataset$estimate
}
}
lapply(BigSplit, WorkFunc)
Any help would be appreciated, Thanks!

Consider using Map (variant to mapply) where you pass both the elements and names of BigSplit. Using Map will output a list of dataframes that you can then row bind at end with do.call(). Below assumes BigSplit is a named list.
WorkFunc <- function(dataset, dataname) {
# Correlation between Year and population Percent
tryCatch({
correlation <- cor.test(dataset$Year, dataset$Percent, method = "pearson")
CIs <- correlation$conf.int
return(data.frame(
Name = dataname,
tvalue = correlation$statistic,
degreeF = correlation$parameter,
pvalue = correlation$p.value,
CI_lower = ifelse(is.null(CIs), NA, CIs[[1]]),
CI_higher = ifelse(is.null(CIs), NA, CIs[[2]]),
corcoeff = correlation$estimate
)
)
}, error = function(e)
return(data.frame(
Name = character(0),
tvalue = numeric(0),
degreeF = numeric(0),
pvalue = numeric(0),
CI_lower = numeric(0),
CI_higher = numeric(0),
corcoeff = numeric(0)
)
)
)
}
# BUILD CORRELATION DATAFRAMES INTO LIST
cor_df_list <- Map(WorkFunc, BigSplit, names(BigSplit))
cor_df_list <- mapply(WorkFunc, BigSplit, names(BigSplit), SIMPLIFY=FALSE) # EQUIVALENT
# ROW BIND ALL DATAFRAMES TO FINAL LARGE DATAFRAME
finaldf <- do.call(rbind, cor_df_list)

Related

R: Create function that iteratively performs some analysis to pairs of rasters, based on their names

I am having 2 sets of raster data and their names are:
ntl_'a number'.tif
pop_'a number'.tif
My goal is to create a function that reads the first pair of rasters (e.g., ntl_1.tif and pop_1.tif), then executes the below code and then repeats the process with the next pair:
library(raster)
library(DescTools)
#create a data.frame of values from the NTL and pop raster data
ntl = raster("path/ntl_1.tif")
vals_ntl <- as.data.frame(values(ntl))
ntl_coords = as.data.frame(xyFromCell(ntl, 1:ncell(ntl)))
combine <- as.data.frame(cbind(ntl_coords,vals_ntl))
pop<-raster("path/pop_1.tif")
pop = resample(pop, ntl, method = 'bilinear')
vals_pop <- as.data.frame(values(pop))
block.data <- as.data.frame(cbind(combine, vals_pop))
names(block.data)[3] <- "ntl"
names(block.data)[4] <- "pop"
block.data <- na.omit(block.data)
block.data = subset(block.data, select = -c(x, y))
# sort by ntl
block.data <-block.data[order(block.data$ntl),]
ntl_vector <- block.data[ , "ntl"]
pop_vector <- block.data[ , "pop"]
#compute gini index
Gini(ntl_vector, pop_vector, unbiased = FALSE)
My issue is with the code inside the function, I do not know how to properly make the syntax (the above code is for a pair of raster while I have hundreds of pairs). Hopefully I can get the results (i.e., the gini coefficient) of every pair in my console or, even better, in a data.frame. The data are here.
library(purrr)
library(fs)
raster_gini <- function(
.ntl = "ntl_1.tif",
.pop = "pop_1.tif",
.rdgal = TRUE
) {
if(.rdgal) {
ntl = raster(.ntl)
vals_ntl <- as.data.frame(values(ntl))
ntl_coords = as.data.frame(xyFromCell(ntl, 1:ncell(ntl)))
combine <- as.data.frame(cbind(ntl_coords,vals_ntl))
pop<-raster(.pop)
pop = resample(pop, ntl, method = 'bilinear')
vals_pop <- as.data.frame(values(pop))
block.data <- as.data.frame(cbind(combine, vals_pop))
#rename the columns
names(block.data)[3] <- "ntl"
names(block.data)[4] <- "pop"
#remove NA values
block.data <- na.omit(block.data)
#remove the columns x & y
block.data = subset(block.data, select = -c(x, y))
# sort by ntl
block.data <-block.data[order(block.data$ntl),]
ntl_vector <- block.data[ , "ntl"]
pop_vector <- block.data[ , "pop"]
#compute gini index
gini <- Gini(ntl_vector, pop_vector, unbiased = FALSE)
c(ntl = .ntl, pop = .pop, gini = gini)
} else {
c(ntl = .ntl, pop = .pop)
}
}
doc_paths_ntl <- fs::dir_ls("path_to_ntl_raster", glob = "*tif*")
doc_paths_pop <- fs::dir_ls("path_to_pop_raster", glob = "*tif*")
result_df <- purrr::map2_dfr(.x = doc_paths_ntl, .y = doc_paths_pop, .f = raster_gini)
result_df <- result_df |>
dplyr::mutate(ntl = basename(ntl)) |>
dplyr::mutate(pop = basename(pop))
result_df

Accessing a variable in a data frame by columns number in R?

I have a data frame as "df" and 41 variables var1 to var41. If I write this command
pcdtest(plm(var1~ 1 , data = df, model = "pooling"))[[1]]
I can see the test value. But I need to apply this test 41 times. I want to access variable by column number which is "df[1]" for "var1" and "df[41]" for "var41"
pcdtest(plm(df[1]~ 1 , data = dfp, model = "pooling"))[[1]]
But it fails. Could you please help me to do this? I will have result in for loop. And I will calculate the descriptive statistics for all the results. But it is very difficult to do test for each variable.
I think you can easily adapt the following code to your data. Since you didn't provide any of your data, I used data that comes with the plm package.
library(plm) # for pcdtest
# example data from plm package
data("Cigar" , package = "plm")
Cigar[ , "fact1"] <- c(0,1)
Cigar[ , "fact2"] <- c(1,0)
Cigar.p <- pdata.frame(Cigar)
# example for one column
p_model <- plm(formula = pop~1, data = Cigar.p, model = "pooling")
pcdtest(p_model)[[1]]
# run through multiple models
l_plm_models <- list() # store plm models in this list
l_tests <- list() # store testresults in this list
for(i in 3:ncol(Cigar.p)){ # start in the third column, since the first two are state and year
fmla <- as.formula(paste(names(Cigar.p)[i], '~ 1', sep = ""))
l_plm_models[[i]] <- plm(formula = as.formula(paste0(colnames(Cigar.p)[i], "~ 1", sep = "")),
data = Cigar.p,
model = "pooling")
l_tests[[i]] <- pcdtest(l_plm_models[[i]])[[1]]
}
testresult <- data.frame("z" = unlist(l_tests), row.names = (colnames(Cigar.p[3:11])))
> testresult
z
price 175.36476
pop 130.45774
pop16 155.29092
cpi 176.21010
ndi 175.51938
sales 99.02973
pimin 175.74600
fact1 176.21010
fact2 176.21010
# example for cipstest
matrix_results <- matrix(NA, nrow = 11, ncol = 2) # use 41 here for your df
l_ctest <- list()
for(i in 3:ncol(Cigar.p)){
l_ctest[[i]] <- cipstest(Cigar.p[, i], lags = 4, type = 'none', model = 'cmg', truncated = F)
matrix_results[i, 1] <- as.numeric(l_ctest[[i]][1])
matrix_results[i, 2] <- as.numeric(l_ctest[[i]][7])
}
res <- data.frame(matrix_results)
names(res) <- c('cips-statistic', 'p-value')
print(res)
Try using as.formula(), for example:
results <- list()
for (i in 1:41){
varName <- paste0('var',i)
frml <- paste0(varName, ' ~ 1')
results[[i]] <-
pcdtest(plm(as.formula(frml) , data = dfp, model = "pooling"))[[1]]
}
You can use reformulate to create the formula and apply the code for 41 times using lapply :
var <- paste0('var', 1:41)
result <- lapply(var, function(x) pcdtest(plm(reformulate('1', x),
data = df, model = "pooling"))[[1]])

How to store values from loop to a dataframe in R?

I am new to R and programming, I want to store values from loop to a data frame in R. I want ker, cValues, accuracyValues values to be stored a data frame from bellow code. I am not able to achieve this, Data Frame is only saving last value not all the values.
Can you please help me with this please.
# Define a vector which has different kernel methods
kerna <- c("rbfdot","polydot","vanilladot","tanhdot","laplacedot",
"besseldot","anovadot","splinedot")
# Define a for loop to calculate accuracy for different values of C and kernel
for (ker in kerna){
cValues <- c()
accuracyValues <- c()
for (c in 1:100) {
model <- ksvm(V11~V1+V2+V3+V4+V5+V6+V7+V8+V9+V10,
data = credit_card_data,
type ="C-svc",
kernel = ker,
C=c,
scaled =TRUE)
pred <- predict(model,credit_card_data[,1:10])
#pred
accuracy <- sum(pred== credit_card_data$V11)/nrow(credit_card_data)
cValues[c] <- c;
accuracyValues[c] <- accuracy;
}
for(i in 1:100) {
print(paste("kernal:",ker, "c=",cValues[i],"accuracy=",accuracyValues[i]))
}
}
Starting from your base code, set up the structure of the output data frame. Then, loop through and fill in the accuracy values on each iteration. This method also "flattens" the nested loop and gets rid of your c variable which conflicts with the built-in c() function.
kerna <- c("rbfdot","polydot","vanilladot","tanhdot","laplacedot",
"besseldot","anovadot","splinedot")
# Create dataframe to store output data
df <- data.frame(kerna = rep(kerna, each = 100),
cValues = rep(1:100, times = length(kerna)),
accuracyValues = NA,
stringsAsFactors = F)
# Define a for loop to calculate accuracy for different values of C and kernel
for (i in 1:nrow(df)){
ker <- df$kerna[i]
j <- df$cValues[i]
model <- ksvm(V11~V1+V2+V3+V4+V5+V6+V7+V8+V9+V10,
data = credit_card_data,
type ="C-svc",
kernel = ker,
C=j,
scaled =TRUE)
pred <- predict(model,credit_card_data[,1:10])
accuracy <- sum(pred== credit_card_data$V11)/nrow(credit_card_data)
# Insert accuracy into df$accuracyValues
df$accuracyValues[i] <- accuracy;
}
Consider Map to build a list of data frames from each pairing of ker and cValues (1:100) generated from expand.grid and row bind all elements together.
k_c_pairs_df <- expand.grid(kerna=kerna, c_value=1:100, stringsAsFactors = FALSE)
model_fct <- function(ker, c) {
model <- ksvm(V11~V1+V2+V3+V4+V5+V6+V7+V8+V9+V10,
data = credit_card_data,
type ="C-svc",
kernel = ker,
C=c,
scaled =TRUE)
pred <- predict(model,credit_card_data[,1:10])
accuracy <- sum(pred== credit_card_data$V11)/nrow(credit_card_data)
print(paste("kernal:",ker, "c=",cValues[i],"accuracy=",accuracyValues[i]))
return(data.frame(kernel = ker, cValues = c, accuracyValues = accuracy))
}
df_list <- Map(model_fct, k_c_pairs_df$ker, k_c_pairs_df$c_value)
final_df <- do.call(rbind, df_list)

Mapply with data.frame/list as the Arguments for the Function

In short, I have a larger function that creates data.frames that are subsets of a larger data.frame and are named after the arguments of the function. It's building data.frames for raw data AND the outputs and the predictive output of Holt-Winters...meaning it is creating multiple data.frames. A small example is the following (though there's not enough intervals here to actually generate a ts class data.frame):
Group <- c("Primary_Group","Primary_Group","Primary_Group","Primary_Group","Primary_Group","Primary_Group","Secondary_Group","Secondary_Group","Secondary_Group","Secondary_Group","Secondary_Group","Secondary_Group","Tertiary_Group","Tertiary_Group","Tertiary_Group","Tertiary_Group","Tertiary_Group","Tertiary_Group")
Day <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
Type <- c("A","A","A","B","B","B","A","A","A","B","B","B","A","A","A","B","B","B")
Value <- c(7,3,10,3,9,4,0,9,3,10,1,6,3,4,10,2,3,1)
df <- as.data.frame(cbind(Group,Day,Type,Value))
Fun <- function(Group,Type, A, B, G){
df <- Data[Data$Group== Group & Data$Type== Type, ]
assign(paste(Group,Type,"_df",sep = ''), df, envir = parent.frame())
df_holtwinters <- HoltWinters(ts(Data[Data$Group== Group & Data$Type== Type, ],
frequency = 365), alpha = A, beta = B, gamma = G)
assign(paste(Group,Type,"_hw",sep = ''), df_holtwinters, envir = parent.frame())
}
You'll notice that the Group and Type are characters, while A, B, G are either numeric or NULL.
If I now have a data.frame composed of lists values, how could I best loop the above function (likely with mapply) to use the values from each column in row one...then each column from row 2 etc - creating several data frames.
argGroup <- c("Primary_Group","Primary_Group","Secondary_Group","Secondary_Group","Tertiary_Group","Tertiary_Group")
argType <- c("A","B","A","B","A","B")
argA <- c(NA, NA, NA, NA, NA, NA)
argB <- c(0.05, 0.05, NA, NA, NA, NULL)
argG <- c(NA, NA, NA, NA, NA, NA)
argGroup[is.na(argGroup)] <- list(NULL)
argType[is.na(argType)] <- list(NULL)
argA[is.na(argA)] <- list(NULL)
argB[is.na(argB)] <- list(NULL)
argG[is.na(argG)] <- list(NULL)
Arguments <- cbind(argType, argType, argA, argB, argG)
Ideally, I would get the following data.frames to generate...
Primary_Group_A_df
Primary_Group_A_hw
Primary_Group_B_df
Primary_Group_B_hw
Secondary_Group_A_df
Secondary_Group_A_hw
Secondary_Group_B_df
Secondary_Group_B_hw
Tertiary_Group_A_df
Tertiary_Group_A_hw
Tertiary_Group_B_df
Tertiary_Group_B_hw
It would also be helpful to understand how to best (most automated way) rbind all the _df together and all the _hw together.
Any help would be amazing and very appreciated. Thanks so much!
Avoid flooding your global environment with many similarly structured objects. Consider using a container such as a list to hold the many dataframes. One useful method is by to subset your dataframe by one or more factor(s) such as Group and Type to return a list of dataframes. Also, don't iterate by rows but merge arguments with data for one pass of arguments per subset.
Specifically, call by twice for df and hw lists. But first, merge the df and Arguments data frames by Group and Type. One challenge is NULL cannot be stored in a data frame, so consider saving "NULL" string and assign temp variables to pass into the HW arguments. Unfortunately, this will cast entire column as character type which you will need to convert with as.numeric for non-NULL values.
Merge
Group <- c("Primary_Group","Primary_Group","Secondary_Group","Secondary_Group",
"Tertiary_Group","Tertiary_Group")
Type <- c("A","B","A","B","A","B")
argA <- c("NULL", "NULL", "NULL", "NULL", "NULL", "NULL")
argB <- c(0.05, 0.05, "NULL", "NULL", "NULL", "NULL")
argG <- c("NULL", "NULL", "NULL", "NULL", "NULL", "NULL")
Arguments <- data.frame(Group, Type, argA, argB, argG, stringsAsFactors=FALSE)
df <- merge(df, Arguments, by=c("Group", "Type"))
Dataframe List (with named df elements)
# ORDER FOR NAMING LATER
df <- with(df, df[order(Type, Group),])
# DATAFRAME LIST
df_list <- by(df, df[c("Group", "Type")], identity)
# RENAME LIST
df_list <- setNames(df_list, unique(paste0(df$Group, "_", df$Type, "_df")))
# REFERENCE ELEMENTS
df_list$Primary_Group_A_df
df_list$Secondary_Group_A_df
df_list$Tertiary_Group_A_df
...
HW List (with named hw elements)
# HW LIST
hw_list <- by(df, df[c("Group", "Type")], function(sub) {
# CONDITIONALLY ASSIGN TEMP VARIABLES
# (BEING SUBSETS: max(arg*)==min(arg*)==mean(arg*)==median(arg*))
if(!is.na(max(sub$argA)) & max(sub$argA) == "NULL") { tmpA <- NULL }
else { tmpA <- max(as.numeric(sub$argA)) }
if(!is.na(max(sub$argB)) & max(sub$argB) == "NULL") { tmpB <- NULL }
else { tmpB <- max(as.numeric(sub$argB)) }
if(!is.na(max(sub$argG)) & max(sub$argG) == "NULL") { tmpG <- NULL }
else { tmpG <- max(as.numeric(sub$argG)) }
# PASS ARGS ONCE PER SUBSET
return(HoltWinters(ts(sub, frequency = 365), alpha=tmpA, beta=tmpB, gamma=tmpG))
})
# RENAME LIST
hw_list <- setNames(hw_list, unique(paste0(df$Group, "_", df$Type, "_hw")))
# REFERENCE ELEMENTS
hw_list$Primary_Group_A_hw
hw_list$Secondary_Group_A_hw
hw_list$Tertiary_Group_A_hw
...
Output (using 3 for HW's frequency to align with posted data)
> hw_list$Primary_Group_A_hw
Holt-Winters exponential smoothing with trend and additive seasonal component.
Call:
HoltWinters(x = ts(sub[c("Group", "Day", "Type", "Value")], frequency = 3), alpha = tmpA, beta = tmpB, gamma = tmpG)
Smoothing parameters:
alpha: 0.2169231
beta : 0.05
gamma: 0.1
Coefficients:
[,1]
a 2.89129621
b 0.08783715
s1 0.54815382
s2 -0.12485260
s3 0.21087038
> hw_list$Secondary_Group_A_hw
Holt-Winters exponential smoothing with trend and additive seasonal component.
Call:
HoltWinters(x = ts(sub[c("Group", "Day", "Type", "Value")], frequency = 3), alpha = tmpA, beta = tmpB, gamma = tmpG)
Smoothing parameters:
alpha: 0.752124
beta : 0
gamma: 0
Coefficients:
[,1]
a 3.691664e+00
b 3.333333e-01
s1 3.333333e-01
s2 -1.480388e-16
s3 -3.333333e-01
> hw_list$Tertiary_Group_A_hw
Holt-Winters exponential smoothing with trend and additive seasonal component.
Call:
HoltWinters(x = ts(sub[c("Group", "Day", "Type", "Value")], frequency = 3), alpha = tmpA, beta = tmpB, gamma = tmpG)
Smoothing parameters:
alpha: 0.3145406
beta : 0
gamma: 0
Coefficients:
[,1]
a 3.022946e+00
b -3.333333e-01
s1 -3.333333e-01
s2 -1.480388e-16
s3 3.333333e-01
You're losing type information by using as.data.frame(cbind(...)),
just use data.frame directly:
Data <- data.frame(
Group = rep(c("Primary_Group", "Secondary_Group", "Tertiary_Group"), each = 6L),
Day = rep(1L:3L, 6L),
Type = rep(rep(c("A", "B"), each = 3L), 3L),
Value = c(7,3,10,3,9,4,0,9,3,10,1,6,3,4,10,2,3,1)
)
Afterwards, I presume you can do the following:
split_data <- split(Data, as.list(Data[, c("Group", "Type")]))
dfs <- do.call(rbind, split_data)
dfs_hw <- lapply(split_data, function(sub_data) {
Map(argA, argB, argG, f = function(A, B, G) {
HoltWinters(ts(sub_data, frequency = 365), alpha = A, beta = B, gamma = G)
})
})
dfs_hw <- do.call(rbind, unlist(dfs_hw, recursive = FALSE))
But I get an error from HoltWinters,
so I can't say for sure.
Also, I think dfs simply has Data again, just reordered.

Applying a function to a list and outputting results

I've got a big database which I've split up by year and created at train and test for each respective year
#split the dataset into a list of datasets
Y <- split(dat_all, dat_all$year)
#create a train and test dataset for all years
#takes Y is inp
create_sets <- function(x){
train_set <- sample(2, nrow(x), replace = TRUE, prob = c(0.7, 0.3))
train <- x[train_set == 1, ]
test <- x[train_set == 2, ]
assign('x', list(train = train, test = test))
}
Ylist <- lapply(Y, create_sets)
To call each item out you use Ylist$'2016'$train
I've made an accuracy ratio function which I can run each list through individually but I am looking for a way to do it all in one to save massive amounts of code (theres 16 years of data)
Below is how I currently create an accuracy ratio for one year
val_train<-Ylist$'2016'$train
val_train$pred<-predict(modf,newdata=Ylist$'2016'$train)
val_train$probs<-exp(val_train$pred)/(1+exp(val_train$pred))
x<-data.frame(rcorr.cens(val_train$probs, val_train$default_flag))
train_AR<-x[2,1]
train_AR
modfull <-ModFit(test)
val_test<-test
val_test$pred<-predict(modf,newdata=test)
val_test$probs<-exp(val_test$pred)/(1+exp(val_test$pred))
x<-data.frame(rcorr.cens(val_test$probs, val_test$default_flag))
test_AR<-x[2,1]
test_AR
AR_Logistic1<-c(train_AR,test_AR,)
AR_Logistic2<-c(train_AR,test_AR) #just in to see if table works
AccuracyRatio<-rbind(AR_Logistic1,AR_Logistic2)
colnames(AccuracyRatio)<-c("Train","Test","All")
AccuracyRatio
Just to clarify I'm trying to run through my whole list through my accuracy ratio and then output the AR for each year for its train and test.
Any help is greatly appreciated
With lapply and wrapping the AR calculations in a function you can summarise the output as below.
Without sample data, I could not test it but let us know if you face any errors.
fn_Calc_AR <- function(yearDat = listInput) {
#yearDat <== Ylist$'2016'
trainDat <- yearDat$train
testDat <- yearDat$test
val_train<- trainDat
val_train$pred<-predict(modf,newdata= trainDat)
val_train$probs<-exp(val_train$pred)/(1+exp(val_train$pred))
x<-data.frame(rcorr.cens(val_train$probs, val_train$default_flag))
train_AR<-x[2,1]
#train_AR
modfull <-ModFit(testDat)
val_test<-testDat
val_test$pred<-predict(modf,newdata=testDat)
val_test$probs<-exp(val_test$pred)/(1+exp(val_test$pred))
x<-data.frame(rcorr.cens(val_test$probs, val_test$default_flag))
test_AR<-x[2,1]
#test_AR
AR_Logistic1<-c(train_AR,test_AR) # removed extraneous comma, previous input c(train_AR,test_AR,)
AR_Logistic2<-c(train_AR,test_AR) #just in to see if table works
AccuracyRatio<-rbind(AR_Logistic1,AR_Logistic2)
colnames(AccuracyRatio)<-c("Train","Test","All")
#confirm yearName is being created
try(yearName <- head(names(x),1)) #retain only year
if(length(yearName) > 0L) {
AR_DF <- data.frame(yearName = yearName , AccuracyRatio,stringsAsFactors=FALSE)
}else{
AR_DF <- AccuracyRatio
}
return(AR_DF)
}
Summarise Output:
AR_Summary = do.call(rbind,lapply(Ylist,fn_Calc_AR))
Aggregate Dataset:
aggregateTrain = do.call(rbind,lapply(Ylist,function(x) x$train))
aggregateTest = do.call(rbind,lapply(Ylist,function(x) x$test))
aggregateList = list(train = aggregateTrain,test = aggregateTest)
AR_AggregateSummary = do.call(rbind,lapply(aggregateList,function(x) fn_Calc_AR(x) ))

Resources