Export Stata regression result to R? - r

My colleague and I chose Stata for regression analysis due to good handling of survey data. We also want to use R for graphics, but do not know how to export Stata regression model.
Within Stata, of course there is return list and ereturn list. But what's the best way to export these results outside of Stata? Direct to R would be ideal, but any intermediate format would be fine as well.

If I were to do this, I would create a Stata dataset with all the regression results using postfile. Then import it into R using, for example, some technique from here. Manipulate within R at your convenience.
See also the user-written command rsource: ssc describe rsource.
Disclaimer: my knowledge of R is rather limited.

I would advise using regsave
ssc install regsave
E.g. after a regression command you could use
regsave, tstat pval ci
to replace the current dataset by the estimation results and save it afterwards.

Related

Call R script from Anylogic and return recults

I need to call a forecast model from R within Anlylogic, and return the resulting outputs in R. It is a specific timeseries that I have built in R, and just copying the coefficients to Anylogic is not efficient. I have seen a couple of older posts on similar questions, but I am not sure I can follow. Any advice would be very appreciated.
I have a regression forecast model that uses predictors to provide a forecast along with Prediction Intervals. I need these outputs to be updated by the different values of the predictors and then used in Anylogic.

R, mitools::MIcombine, what is the reason for no p-values?

I am currently running a simple linear regression model with 5 multiply imputed datasets in R.
E.g. model <- with(imp, lm(outcome ~ exposure))
To pool the summary estimates I could use the command summary(mitools::MIcombine(model)) from the mitools package. However, this does not give results for p-values. I could also use the command summary(pool(model)) from the mice package and this does give results for p-values.
Because of this, I am wondering if there is a specific reason why MIcombine does not produce p-values?
After looking through the documentation, it doesn't seem like there is a particular reason that the mitools library doesn't provide p-values. Although, the package's focus is on imputation, not model results.
However, you don't need either of these packages to see your results–along with the per model p-values. I started writing this as a comment but decided to include the code. If you weren't aware...you can use base R's summary. I realize that the output of mice is comparative, as is mitools. I thought it was important enough to mention this, as well.
If the output of your call is model, then this will work.
library(tidyverse)
map(1:length(model), ~summary(model[.x]))

How to run a dynamic linear regression in R?

I am new to using R as I usually use Stata. I want to estimate a state space model on some time series data with time varying coefficients. From what I have gathered this is not possible to do in Stata.
I have downloaded the dlm package in R and I am trying to run the dlmModReg command to regress my dependent variable on a single explanatory variable. I would like to allow the intercept and beta coefficient to vary over time.
If anyone could show me an example of the code I want to run I think that would be enough for me to work out how to do this. The examples I have found online are vague or use terminology that I am not familiar with as a new R user. Any help or comments are greatly appreciated.

How to export multivariate forecast results from R to excel

I'm terribly new with R, so I apologize if there's a way to do this using a slight variation of an existing code/package.
I've created yearly forecasts of a variable (student enrollment) for 129 countries using the predict command, and then i have them binded. I've done this because I'm forecasting using a multivariate regression.
Here's what I'm doing (if this helps)
`fm1=lm(log(y+1)~Var.Ind)
XNew=data.frame(Var.Ind)
(rse<-summary(fit)$sigma(fm1)* df.residual(fm1))/2
rse<-summary(fm1)$sigma
yhat1=exp(predict(fm1,XNew)+rse*rse/2)-1
pos2014=which(Var.Ind[,1]==c(2014))
Var.Ind.2015=model.matrix(~as.matrix(Imp.Data4[pos2014,-2])-1)
head(Var.Ind.2015)
Var.Ind.2015=data.frame(Var.Ind.2015)
Var.Ind.2015.Ord=as.data.frame(Var.Ind.2015[order(Var.Ind.2015[,3],Var.Ind.2015[,1]), ])
head(Var.Ind.2015.Ord)
X.New.New=data.frame(cbind(model.matrix(~as.matrix(Var.Ind.2015.Ord))))
head(X.New.New)
ColNames.N=ColNames[-2]
colnames(X.New.New)=c("Int",ColNames.N,"Lag1","Lag2")
head(X.New.New)
Beta.Coef=matrix(as.numeric(fm1$coefficients),ncol=1)
Beta.Coef
Pred2015=as.data.frame(cbind(X.New.New[,3],exp(as.matrix(X.New.New)%*%Beta.Coef+rse*rse/2)-1))
dim(Pred2015)
colnames(Pred2015)=c("country","Yhat")
*And so on for subsequent years until 2030)
cbind(Pred2015, Pred2016, Pred2017, Pred2018, Pred2019)`
I need to figure out if there is a way to make sense of these results:
a) how to export the forecast results to excel
b) alternatively, if I could put these results into a table using R.
Also, these results do not appear in the Global Environment, only in the results section of the program, which is why I am not asking how to export data, but rather these specific results.
As previously mentioned, my coding knowledge is limited to my 1 week experience with R (I usually work with STATA).
Any help would be greatly appreciated!

How can I specify 'unstructured' in lme function?

I'm trying to fit a mixed model in R.
Actually I have a Stata code and want to do the same thing using R.
The Stata code is
xtmixed laz c.x1##i.j4alloc_n c.x2##i.j4alloc_n ||childuid:age_m, cov(uns) var
I can't find how to specify 'unstructured' correlation structure in R.
In the R help file, there is no 'unstructured'
https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/corClasses.html

Resources