Try this: sapply(by(x, x$basestudy, lm, formula = ef ~ quartile), coef)
On Tue, Oct 26, 2010 at 1:08 PM, Small Sandy (NHS Greater Glasgow & Clyde) < sandy.sm...@nhs.net> wrote: > Hi > > I need some help getting results from multiple linear models into a > dataframe. > Let me explain the problem. > > I have a dataframe with ejection fraction results measured over a number of > quartiles and grouped by base_study. > My dataframe (800 different base_studies) looks like > > > afvtprelvefs > basestudy quartile ef ef_std entropy > CBP0908020 1 21.6 0.53 3.27 > CBP0908020 2 32.5 0.61 3.27 > CBP0908020 3 30.8 0.63 3.27 > CBP0908020 4 33.6 0.37 3.27 > CBP0908022 1 42.4 0.52 1.80 > CBP0908021 1 29.4 0.70 2.63 > CBP0908021 2 29.2 0.42 2.63 > CBP0908021 3 29.7 0.89 2.63 > CBP0908021 4 29.3 0.50 2.63 > CBP0908022 2 45.7 1.30 1.80 > ... > > What I want to do is apply a weighted linear fit to the results from each > base study and get the gradient out of it. I then want to plot the gradient > against the entropy (which is constant for each base study). > > I can get apply a linear fit with > > > fits <- by(afvtprelvefs, afvtprelvefs$basestudy, function (x) lm (ef ~ > quartile, data=x, weights=1/ef_std)) > > but how do I get the results from that into a dataframe which I can use? > > I thought I might get somewhere with > > sapply(fits, "[[", "coefficients") > > But that doesn't give me the basestudy separately so that I can match up > the results with the entropy results. > > I am sure this must have been answered somewhere before but I have been > unable to find a solution. > Many thanks for your help > > Sandy Small > NHS Greater Glasgow and Clyde > > > > ******************************************************************************************************************** > > This message may contain confidential information. If ...{{dropped:20}} ______________________________________________ R-help@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.