Sherlene Enriquez-Savery
2011-Oct-25 19:21 UTC
[R] regression using GMM for mulltiple groups
Inthe code below I was trying to to obtain the GMM estimates for CAPM (REGRESSION) for 36 stocks each have 180 observations,however it only gives me one output rather than 36. In SAS i would just put in a *By statement*. I have a variable TICKER that categorize them into 36 groups. *How can I obtain all 36 output instead of just one.* ** DataMat<-read.table(file="C://Users//ssenriqu//Dropbox//Work//Risk//data//CRSP//CRPS 2 original1.csv",sep=",",dec=".",header=TRUE) require(gmm) Returns <- DataMat[1:6480,"Returns"] VWRET <- DataMat[1:6480,"VWRET"] RF <- DataMat[1:6480,"RF"] z<-as.matrix(Returns-RF) zm<-as.matrix(VWRET-RF) res <- gmm(z ~ zm, x = zm) coef(res) -- Sherlene Enriquez-Savery, Doctoral Candidate Department of Mathematics & Statistics University of South Florida 4202 East Fowler Avenue Tampa,FL, 33620 (813) 974-5439 (813) 974 6126 Fax ssenriqu@usf.edu ssenriqu@mail.usf.edu [[alternative HTML version deleted]]
There are a few ways to do this. One is to use the lmList() function in the nlme package and use conditioning in the model formula. Another is to use the plyr package to create a list of models from which you can extract pieces of output from each model fit to the data subsets; for example, library('plyr') # dlply stands for data frame input, list output - the first letter # of the name designates the input type, the second the output type mymods <- dlply(DataMat, .(TICKER), function(d) lm(VWRET ~ RF, data = d)) # from which you could do such things as CAPMcoefs <- ldply(mymods, function(m) coef(m)) resids <- ldply(mymods, function(m) resid(m)) # among many others. Using lmList(), library('nlme') CAPMods <- lmList(VWRET ~ RF | TICKER, data = DataMat) If you want separate estimates of MSE for each model, put in pool FALSE as an additional argument - I believe the default is to pool the residual variance across all the models. Check its help page for details. lmList() also returns a list of lm model objects, just like dlply() did above. HTH, Dennis On Tue, Oct 25, 2011 at 12:21 PM, Sherlene Enriquez-Savery <ssenriqu at mail.usf.edu> wrote:> Inthe code below ?I was trying to to obtain the GMM estimates for CAPM > (REGRESSION) for 36 stocks each have 180 observations,however it only gives > me one output rather than 36. > > In SAS i would just put in a *By statement*. ?I have a variable TICKER that > categorize them into 36 groups. > > *How can I obtain all 36 output instead of just one.* > ** > > DataMat<-read.table(file="C://Users//ssenriqu//Dropbox//Work//Risk//data//CRSP//CRPS > 2 original1.csv",sep=",",dec=".",header=TRUE) > require(gmm) > Returns <- DataMat[1:6480,"Returns"] > VWRET <- DataMat[1:6480,"VWRET"] > RF <- DataMat[1:6480,"RF"] > z<-as.matrix(Returns-RF) > zm<-as.matrix(VWRET-RF) > res <- gmm(z ~ zm, x = zm) > coef(res) > > > -- > > Sherlene Enriquez-Savery, Doctoral Candidate > > Department of Mathematics & Statistics > > University of South Florida > 4202 East Fowler Avenue > Tampa,FL, 33620 > > (813) 974-5439 > > (813) 974 6126?Fax > > ssenriqu at usf.edu > > ssenriqu at mail.usf.edu > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >