I would like to run some Matlab scripts. Nevertheless we don't have the Matlab licence so it is necessary a conversion from Matlab to R language. Unfortunately I'm totally new in Matlab but not in R. Is it possible to read Matlab scripts using R or is there an easy way to translate Matlab scripts in R?
Rewriting from one language to another can be a painstaking process, especially because your have to take great care that the outcomes of both sets of codes are the same. I see roughly four approaches:
Digest the goal of the scripts, put aside the matlab code, and rewrite in R
Try and mimic the matlab code in R
Run the matlab code in octave, and interface with R
Run the code in Octave entirely
These are roughly in order of amount of work. If you just want to get the Matlab code working, definitely use Octave, which should run the code with minimal changes. If you want to convert the code to R, and continue developing in R, I would go for the first option. In that way you can leverage the real strenghts of R, as R is quite different (link with info, comparison R and matlab). But it does take the largest amount of time. Even if you reimplement in R, I would recommend getting the code running in Octave to be able to see if your results in R fit with the Matlab code.
Related
I have defined some R functions in R studio which has some complicated scripts and a lot of readlines. I can run them successfully in R studio. Is there any way, like macros to transfer these user-defined functions to SAS 9.4 to use? I am not pretty familiar with SAS programming so it is better just copy the R functions into SAS and use it directly. I am trying to figure out how to do the transformation. Thank you!
You can't natively run R code in SAS, and you probably wouldn't want to. R and SAS are entirely different concepts, SAS being closer to a database language while R is a matrix language. Efficient R approaches are terrible in SAS, and vice versa. (Try a simple loop in R and you'll find SAS is orders of magnitude faster; but try matrix algebra in R instead).
You can call R in SAS, though. You need to be in PROC IML, SAS's matrix language (which may be a separate license from your SAS); once there, you use submit / R to submit the code to R. You need the RLANG system option to be set, and you may need some additional details set up on your SAS box to make sure it can see your R installation, and you need R 3.0+. You also need to be running SAS 9.22 or newer.
If you don't have R available through IML, you can use x or call system, if those are enabled and you have access to R through the command line. Alternately, you can run R by hand separately from SAS. Either way you would use a CSV or similar file format to transfer data back and forth.
Finally, I recommend seeing if there's a better approach in SAS for the same problem you solved in R. There usually is, and it's often quite fast.
So I'm taking a statistical course at the moment. And one assignment is to create a plot like seen below. My professor uses the Stata program, and have said that it pretty much can be done my the following command:
power twomeans 0 5 , sd1(9) sd2(9) alpha(0.05) power(0.90)
I am not using Stata, so I don't have the option to do that command. But I have been using R for all other exercises where I have been able to pretty much replicate everything. But not this time. I have searched and searched, but there doesn't seem to be any simple command to create this. So am I just missing something, I do I actually have to build it myself ?
I have an R script that I need to understand better - what it's doing, why it's doing what it's doing, etc. Problem is, I'm not familiar with R. I am, however, familiar with Matlab. So I'm hoping there's a way to port this R script into Matlab such that I can run through this R script in a way how to use different software.
I don't just want to run the R script and have the output sent to Matlab for further use, I want to be able to open an R script in Matlab and be able to treat it like a .m script.
Failing that, at least some way to run through the R script (using R in Windows) that doesn't involve stepping through every single line in debugging and then having to type print([variable name]) at every step to see what the function did.
Is it possible to call Stata functions from R?
Not directly, i.e. there is no package I am aware of that implements a bridge.
You can always call external programs using system() but that is neither elegant nor efficient. That said, you could prepare data in R, write it out, call Stata and then read the results in; see help(system).
There's now an RStata package on CRAN that bridges R and Stata.
The real problem is that Stata doesn't have an interactive interpreter you can pass arguments to.
Dirk is right; you can just go ahead and write the data to a common format
(if size is large and speed is an issue, fixed width is safe), but you can also just use .dta throughout the process, using read.dta in R and natively reading in Stata.
Also, in R you can call to the system() you can pass a do file or a string containing a bunch of Stata commands.
So, generally, trying to use Stata for this or that task may or may not be worth it, especially if an R equivalent is close by.
Have been using Stata to run negative binomial regressions in a replication. Not sure what is under the hood on how Stata does this, but wanted to know if there is an R function/package that does the same thing? The R will give me a better idea of how this works, since I can see the code.
Look into the glm.nb function in the MASS package. If you're interested in what's happening "under the hood," you can see the source code of the function by just entering its name at the command prompt.
If you're more comfortable using R, then that's probably the way to go; however, if you're interested in what's "under the hood" in Stata, you can always see what's going on in much the same way as in R by using
set trace on
to see what code is running (or download tr from SSC) or using
viewsource xtnbreg.ado
to see the actual code that is run by xtnbreg.
If you're interested in how Stata is calculating the results in xtnbreg there is a detailed discussion of the likelihood function in the [XT] manual page 367-370 with references included.