is there an R function for Stata's xtnbreg? - r

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.

Related

Statistical power in R as in Stata

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 ?

Markov Switching-Autoregressive in R

I'm trying to replicate this paper but using different time period
https://www.dropbox.com/s/edwdpgwsbli93f1/SM35%282%29-09-modelling.pdf?dl=0.
This paper is about detecting regime shifts in Malaysian currency i.e the ringgit. From what I understand it uses Markov Switching-Autoregressive method (MS-AR). I've been trying to replicate this method in R, but to no success. There has been some question asking about it lately which can be found here
Error when using msmFit in R
Basically I'm having the same problem. When I tried to do the MS-AR the error came out. I'm not sure what the exact calculation for the msmFit, but from some examples online they use this to get the fit for MS-AR. So my question is, is it actually possible to do MS-AR(p) in R? Is there any other software besides R or Eviews 8 (since I don't have this at the moment) that can actually do this?
Thank you. Really appreciate your insight.
link msmFit: http://cran.r-project.org/web/packages/msm/msm.pdf
There is a package for MATLAB called MS_Regress, it should do the job:
https://sites.google.com/site/marceloperlin/matlab-code/ms_regress---a-package-for-markov-regime-switching-models-in-matlab
I was trying to fit the MS-AR model in R, but I get the same error message. Could you provide us with a link to the examples you found about getting the fit in R?

Is there any Python equivalent of R's biglm?

I have used biglm in R and found it very useful. Now I need the same type of functionality in python. Any ideas? I have seen that patsy/statsmodels has an incremental mode, but have not been able to find any samples to copy/adapt. Any pointers would be much appreciated.
from a related answer of Nathaniel Smith on the statsmodels mailing list
My incremental LS code might be useful here, it's basically the same
problem:
https://github.com/njsmith/pyrerp/blob/master/pyrerp/incremental_ls.py#L330
The new X'X is the sum of the old X'Xs, then you have to re-do the
scaling and inversion to get the new vcov matrix for the estimates.
Should be doable so long as you know how many data points are in each
and the various sums-of-squares. (The code I linked has some extra
complexity because of handling a particular sort of heteroskedasticity
via FGLS, but it can pretty much be ignored.)
statsmodels doesn't have anything in this area yet.
There is an incremental OLS function in statsmodels, however that was written as helper function for cusum tests (in memory) and hasn't been used or checked for any other purpose:
http://statsmodels.sourceforge.net/devel/generated/statsmodels.stats.diagnostic.recursive_olsresiduals.html

How does r calculate the p-values in logistic regression

What type of p-values do R calculate in a binomial logistic regression, and where is this documented?
When i read the documentation for ?glm() I find no reference to the calculation of the p-values.
The p-values are calculated by the function summary.glm. See ?summary.glm for a (very brief) bit about how those are calculated.
For more information, look at the source code by typing
summary.glm
at the R command prompt. There you will find the lines of code where an object pvalue is created. Follow the code back to see how the components of the p-value calculation are (conditionally) calculated.
The authors of R wrote the help system with several principles in mind: compactness (don't write more than is needed, it's not a textbook), accuracy, and a curious and well-educated audience. It really was written for other statisticians. The "curious" part of that opening sentence was included to raise the question why you did not also follow the various links in the ?glm page: to summary.glm where you would have found one answer to your ambiguous question or to anova.glm where you would have found another possible answer. The help-authors do expect that you will follow those links and read the whole page and execute the examples. You will notice that even after you get to summary.glm that there is no mention of "binary logistic regression" since they pretty much assume that you are well-grounded in statistics and have copy of McCullagh and Nelder handy, or if not that you will go read the references.
The other principle: sometimes it is the code itself (given the open-source nature of R) that performs the documentation. Technically glm doesn't print anything and print.glm doesn't print p-values. It would be print.summary.glm or print.anova.glm that would be doing any printing. Part of learning R is learning that the results printed to the console will have gone through a eval-print loop and that output can be tailored with object-class-specific functions.
These assumptions are just part of what many people see as a "steep learning curve for R" (although I would have called it a shallow curve if plotted with time/effort on x-axis.)

how to convert Matlab scripts in R

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.

Resources