0

I have independent and dependent datasets. I want to test all possible relationships between dependent and independent variables. In my previous post (How to replicate a function using mapply with multiple arguments to calculate the power of a method?), I wanted to do power analysis using simulation data. Now, I want to analyze real data using the same function. The problem is that the test_function needed more time as my dataset is big (dimension of each data set greater than 10000 X 40000). Also, I want to use parallel computing to speed up the calculation. I have found that the bigstatsr package (https://privefl.github.io/bigstatsr/index.html) can handle matrices that are too large to fit in memory. Moreover, I want to avoid expand.grid as it is also computationally expensive for big data. I did not find any post that can use two datasets simultaneously using the bigstatsr package and estimate parameters parallelly. Datasets examples and code are given below:


# dependent dataset
test_A <- data.frame(matrix(rnorm(100), nr=10, nc=10))
# independent dataset
test_B <- data.frame(matrix(sample(c(0,1,2), 500, replace = TRUE), nr=50, nc=10))
# Find all combination using dependent and independe datasets's variables
A_B_pair <- subset(expand.grid(c1=names(test_A), c2=names(test_B), 
                               stringsAsFactors = FALSE))
# Main function to estimate the parameter and p-values 
test_function <- function(test_A, test_B, x,y){
  c1 <- test_A [[x]]
  c2 <- test_B[[y]]
  Data <- data.frame(1, XX=c1, YY=c2)
  
  model_lm <- lm(YY ~ XX, Data)
  est_lm <- as.numeric(model_lm$coefficients)[2]
  pvalue_lm <- as.numeric(summary(model_lm)$coeffi[,4][2])
  
  return(unlist(data.frame(lm.estimator = est_lm, lm.pvalue =pvalue_lm)))
}
# Final output
output <- mapply(test_function, MoreArgs = list(test_A, test_B),
                 x = A_B_pair$c1, y = A_B_pair$c2)

Edit: I want to apply my proposed method to estimate the parameters and compare the results with the lm method. My proposed method is given below:

library(pracma)
Proposed_method<- function(Data, Beta) 
{ 
  n = dim(Data)[1]
  Median <- t(apply(Data,2,median))
  Dist <- sqrt(rowSums((Data - as.matrix(rep(1,dim(Data)[1]))%*%Median)^2))
  Data0 <- as.matrix(Data[which(Dist <= as.numeric(quantile(Dist, p=.45, na.rm = TRUE))),])
  Yo <- as.matrix(Data0[,dim(Data0)[2]])
  Xo <- as.matrix(Data0[,-dim(Data0)[2]])
  Gama0 <- as.numeric(pinv(crossprod(Xo, Xo))%*%crossprod(Xo, Yo))
  Sigma2o <- var(Yo)
  Y <- as.matrix(Data[,dim(Data)[2]])
  X <- as.matrix(Data[,-dim(Data)[2]])
  
  DiffTol = 0.0001;
  DiffNorm = +10000;
  Iter = 0;
  ###########While loop################
  while (DiffNorm > DiffTol)
  {
    Const <- sqrt(2*pi*Sigma2o)
    devmat <- (Y-X%*%Gama0)
    Squaremat <- as.matrix(apply(devmat, c(1,2), function(x) x^2))
    Gauss <- exp(-Squaremat/(2*as.numeric(Sigma2o)))/as.numeric(Const)
    Wbeta <- exp(-(Beta*((Y-X%*%Gama0)*(Y-X%*%Gama0)))/(2*as.numeric(Sigma2o)))
    ONE1 <- rep(1,dim(X)[2]);
    Xb <- (X*(Wbeta%*%ONE1)) 
    Gama <- as.numeric(pinv(crossprod(X, Xb))%*%crossprod(Xb, Y)) 
    hedprod <- (Y-X%*%Gama)*(Y-X%*%Gama) 
    tWbeta <- as.matrix(t(Wbeta)) 
    One_1 <- as.matrix(rep(1,dim(X)[1])) 
    Sigma2 <- (tWbeta%*%hedprod)*pinv(tWbeta%*%One_1)
    
    LHb<-(sum(Gauss^Beta)/n-1)/Beta
    LH<-prod(Gauss)
    ##########
    Norm2 <- ((sum(Gama*Gama))^0.5 + abs(Sigma2))
    DiffNorm <-((sum((Gama-Gama0)*(Gama-Gama0)))^0.5 + abs(Sigma2 - Sigma2o))/Norm2
    ###
    Gama0 = Gama
    Sigma2o=Sigma2
    Iter = Iter + 1 
  }
  return(list(Gama=Gama,Sigma2=Sigma2,Wt=Wbeta,LHb=LHb,LH=LH))
}
# independent variable dataset
test_A <- data.frame(matrix(sample(c(0,1,2), 500, replace = TRUE), nr=10, nc=50))
# dependent variable dataset
test_B <- data.frame(matrix(rnorm(1000), nr=10, nc=100))
# Find all combination using dependent and independe datasets's variables
A_B_pair <- subset(expand.grid(c1=names(test_A), c2=names(test_B), 
                               stringsAsFactors = FALSE))
# Main function to estimate the parameter and p-values by proposed method and lm 
test_function <- function(x, y){
  c1 <- test_A[[x]]
  c2 <- test_B[[y]]
  Data <- data.frame(1, XX=c1, YY=c2)
  nn <- dim(Data)[1]
  Beta = 0.1
  Omit = 2
  ResL1 <- Proposed_method(Data, Beta)
  ResL0 <- Proposed_method(as.matrix(Data[,-Omit]), Beta)
  LR0 <- (-nn)*log(ResL1$Sigma2/ResL0$Sigma2)
  
  # Proposed estimator
  Proposed_estimator <- (ResL1$Gama)[2]
  Proposed_pvalue <- as.numeric(pchisq(q=LR0, df=1, lower.tail = FALSE))
  
  #lm model
  model_lm <- lm(YY ~ XX, Data)
  est_lm <- as.numeric(model_lm$coefficients)[2]
  pvalue_lm <- as.numeric(summary(model_lm)$coeffi[,4][2])
  
  return(unlist(data.frame(lm.estimator = est_lm, lm.pvalue =pvalue_lm, Proposed_estimator,Proposed_pvalue)))
}

# Output:
output <- mapply(test_function, x = A_B_pair$c1, y = A_B_pair$c2)
# transpose the output
output_t <- data.frame(t(output))

# Final output
output_final <- cbind(A_B_pair, output_t)
output_final <- structure(list(c1 = c("X1", "X2", "X3", "X4", "X5"), c2 = c("X1", 
"X1", "X1", "X1", "X1"), lm.estimator = c(-0.855708052636761, 
0.227250280548332, -0.128955946232531, 0.171650221327542, -0.701027831473379
), lm.pvalue = c(0.0361141129937136, 0.646905371365762, 0.816730073250761, 
0.780290676037238, 0.261013977519426), Proposed_estimator = c(-0.879232513006948, 
0.242368232504351, -0.110999951753211, 0.174574390311335, -0.76456493319124
), Proposed_pvalue = c(0.0131801103443272, 0.583155149115837, 
0.870570103632653, 0.783460676404866, 0.154142429946211)), row.names = c(NA, 
5L), class = "data.frame"))

How can I apply bigstatsr and parallelly compute this function to get the outputs? Thank you so much for your effort and help.

  • `test_A` and `test_B` should have the same number of rows, right? – F. Privé Nov 23 '21 at 13:54
  • @F. Privé, Yes, the row number should be the same for test_A and test_B datasets – user8129108 Nov 24 '21 at 03:06
  • I still don't think you have a problem of memory here. You can always transform `test_A` and `test_B` to some FBM so that you don't need to copy the data when using parallelization. But other from that, you should try to optimize your code so that it runs faster. – F. Privé Nov 24 '21 at 10:40
  • I have tried to use big_apply instead of `mapply` but it's not working. Could you please help me to optimize my code and apply bigstatsr for my function to get the desired outputs? Thank you for your effort.@F.Privé – user8129108 Nov 24 '21 at 11:01
  • You don't need big_apply here, you can just loop through the variables of the two datasets. Sorry, but I don't have time to benchmark and make your code faster. – F. Privé Nov 24 '21 at 12:25

1 Answers1

0

I don't think there is really a problem of size here (memory-wise), but just a computation time problem.

I think you just want to do some univariate testing. For that, you can use function big_univLinReg:

library(bigstatsr)
X <- as_FBM(test_B)
NCORES <- nb_cores()

k <- 1  ## replace by loop here
stats <- big_univLinReg(X, test_A[[k]], ncores = NCORES)
pval <- predict(stats, log10 = FALSE)

This function should be quite fast, and gives you all the coefficients for all variables in test_B. Then you only need to loop over the variables in test_A.

F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • Thank you for your reply@F. Privé. I want to use my own function instead of the `lm` function to estimate the parameter and p-value. Also, I want to add two columns to the output dataset. One is the names of test_A and another is the names of the test_B dataset. – user8129108 Nov 23 '21 at 15:13
  • Output will be like this: `output_t <- data.frame(t(output)); output_final <- cbind(A_B_pair, output_t); final_output <- structure(list(c1 = c("X1", "X2", "X3", "X4", "X5"), c2 = c("X1", "X1", "X1", "X1", "X1"), lm.estimator = c(-0.0422342260166708, -0.0187980183564189, 0.192428884676606, -0.0257373964876148, 0.0673635213446617), lm.pvalue = c(0.701851660233888, 0.876046574990813, 0.0962188742808562, 0.817364911991616, 0.54800706638316)), row.names = c(NA, 5L), class = "data.frame")` – user8129108 Nov 23 '21 at 15:17
  • Please edit your question to have something closer to what you actually want to do then. – F. Privé Nov 23 '21 at 21:56
  • Dear @F. Privé, I have added a part in my question named "Edit". Please see this now and give your kind suggestion to adapt the code by applying bigstatsr. – user8129108 Nov 24 '21 at 03:05