haghish / rcall

Seamless interactive R in Stata. rcall allows communicating data sets, matrices, variables, and scalars between Stata and R conveniently
http://www.haghish.com/packages/Rcall.php
91 stars 29 forks source link

matsize too small in vanilla #11

Closed luispfonseca closed 5 years ago

luispfonseca commented 5 years ago

I'm calling R using the vanilla mode and I have a command that works fine for smaller datasets but I get an error as soon as I'm dealing with a matrix above 400 entries in R. I'm calling R in vanilla mode and dealing with moving data in and out of Stata and R by saving the files myself.

The error I get is matsize too small. I wasn't expecting this error since I'm calling R in vanilla mode and taking care of bringing data back and forth through files and not through rcall.

Something strange I noticed in the trace is the following:

      di _n "{title:[5/5] rclass return}" _n "The final step is returning objects from R to Stata" _n
      di "{browse stata.output}"
      }
    - call_return using "stata.output" , `debug'
    = call_return using "stata.output" , 
matsize too small
    You have attempted to create a matrix with too many rows or columns or attempted to fit a model with too many variables.  You need to increase matsize; it is currently
    400.  Use set matsize; see help matsize.

Since I'm calling it in vanilla mode, I wasn't expecting it to be working as it says there on preparing to return objects from R to Stata. Do you know if this is intended, or I'm misunderstanding how it is working in the background?

Thanks!

haghish commented 5 years ago

This is true. This error occurs in Stata and not R. When Stata starts to define a very large matrix - that was written by R - it generates this error, due to its matsize limit.

The question is do you actually want this huge matrix to be returned to Stata? if so, you can do the following:

  1. save the matrix in a file, like a CSV file, within your R code
  2. make sure that you remove the matrix from R's memory at the end of your R-script file. If you don't, rcall will attempt to return it to Stata, which results in this problem.

PS, vanilla mode only means that it starts with a fresh R environment. But the data communication is the same at the end of each call, i.e. all matrices and scalars are returned to Stata.

luispfonseca commented 5 years ago

Thanks! I was confusing things regarding the vanilla mode. Makes sense.

Clearing the matrix from R at the end of the R script made a huge difference in this case. Not just it avoids those errors, but there are speed gains of 95+% when the data being moved starts getting large. I was already moving the data back through csv files.

haghish commented 3 years ago

should be solved in rcall 3.0.0 BETA