JulieV
2012-Jan-22 20:31 UTC
[R] Calculating & plotting a linear regression between two correlated variables
Hi, I have a Community (COM) composed of 6 species: A, B, C, D, E & F. The density of my Community is thus (Eq.1): dCOM = dA + dB + dC + dE + dF I would like to calculate and plot a linear regression between the density of each of my species and the density of the whole community (illustrating how the density of each species varies with variations of the whole community). For example, I would like to plot dA = a * dCOM + b, with a and b the slope and intercept of the regression. The problem is that dA and dCOM are correlated because dA contributes to values of dCOM (see Eq.1 above), and thus I'm probably not allowed to use a "simple" linear regression (because parametric statistics do not allow for correlated observations).>From what I red (eg, www.ats.ucla.edu/stat/r/faq/spatial_regression.htm),Linear Mixed Models allow for correlated observations by adding a correction to the values. The webpage also says that we can use the correlation option in the lme function (nlme package) to find the type of correction to be used, but I can?t figure out how to do this for my dataset. Can someone help me please ? You will find an example of my dataset below (density of species A [dA] and density of the whole community [dCOM]) and my R script. Example from my dataset: dA dCOM 0.611 0.73 0.474 0.669 0.203 0.388 0.011 0.213 0.407 0.722 0.148 0.437 0.084 0.281 0 0.054 0.402 0.93 0.044 0.285 0.011 0.147 0 0.091 0.547 0.767 0.559 0.699 0.321 0.441 0.084 0.262 0.428 0.761 0.234 0.398 0.019 0.191 0 0.053 0.302 0.509 0.06 0.213 0.045 0.184 0.033 0.118 0.374 0.585 0.256 0.5 0.22 0.401 0.136 0.275 0.503 0.684 0.48 0.617 0.257 0.387 0.038 0.159 0.382 0.611 0.192 0.393 0.047 0.243 0.036 0.188 R script for this example: dummy <- rep(1,36) # grouping variable in our data model1 <- lme(fixed = dA ~ dCOM, data = bb1, random = ~ 1 | dCOM/dummy, correlation= ?,method = "ML") Thank you very much. Julie. -- View this message in context: http://r.789695.n4.nabble.com/Calculating-plotting-a-linear-regression-between-two-correlated-variables-tp4319051p4319051.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2012-Jan-23 06:35 UTC
[R] Calculating & plotting a linear regression between two correlated variables
Hi Julie, Mixed effects models are typically used to allow for correlations between observations of the outcome variable---the fact that you are trying to model dA ~ dCOM rather assumes you expect some sort of association between the two. It is not exactly clear what you want to deal with using a correlation structure, but it seems to me your model is this: dA = a * (dA + dB + dC + dE + dF) + b which is a little circular because dA is explaining itself. Without knowing more, I would think about something like: dA = a1 * dB + a2 * dC + a3 * dE + a4 * dF + b if you want a single slope for all a1 - a4 effects, you could move to a structural equation modelling framework and impose the constraint that: a1 = a2 = a3 = a4 in the parameter estimation. Cheers, Josh On Sun, Jan 22, 2012 at 12:31 PM, JulieV <sharkette002 at hotmail.com> wrote:> Hi, > I have a Community (COM) composed of 6 species: A, B, C, D, E & F. > The density of my Community is thus (Eq.1): dCOM = dA + dB + dC + dE + dF > > I would like to calculate and plot a linear regression between the density > of each of my species and the density of the whole community (illustrating > how the density of each species varies with variations of the whole > community). > For example, I would like to plot dA = a * dCOM + b, with a and b the slope > and intercept of the regression. > > The problem is that dA and dCOM are correlated because dA contributes to > values of dCOM (see Eq.1 above), and thus I'm probably not allowed to use a > "simple" linear regression (because parametric statistics do not allow for > correlated observations). > > >From what I red (eg, www.ats.ucla.edu/stat/r/faq/spatial_regression.htm), > Linear Mixed Models allow for correlated observations by adding a correction > to the values. The webpage also says that ?we can use the correlation option > in the lme function (nlme package) to find the type of correction to be > used, but I can?t figure out how to do this for my dataset. > > Can someone help me please ? > You will find an example of my dataset below (density of species A [dA] and > density of the whole community [dCOM]) and my R script. > > > Example from my dataset: > dA ? ? ?dCOM > 0.611 ? 0.73 > 0.474 ? 0.669 > 0.203 ? 0.388 > 0.011 ? 0.213 > 0.407 ? 0.722 > 0.148 ? 0.437 > 0.084 ? 0.281 > 0 ? ? ? 0.054 > 0.402 ? 0.93 > 0.044 ? 0.285 > 0.011 ? 0.147 > 0 ? ? ? 0.091 > 0.547 ? 0.767 > 0.559 ? 0.699 > 0.321 ? 0.441 > 0.084 ? 0.262 > 0.428 ? 0.761 > 0.234 ? 0.398 > 0.019 ? 0.191 > 0 ? ? ? 0.053 > 0.302 ? 0.509 > 0.06 ? ?0.213 > 0.045 ? 0.184 > 0.033 ? 0.118 > 0.374 ? 0.585 > 0.256 ? 0.5 > 0.22 ? ?0.401 > 0.136 ? 0.275 > 0.503 ? 0.684 > 0.48 ? ?0.617 > 0.257 ? 0.387 > 0.038 ? 0.159 > 0.382 ? 0.611 > 0.192 ? 0.393 > 0.047 ? 0.243 > 0.036 ? 0.188 > > R script for this example: > > dummy <- rep(1,36) ?# ?grouping variable in our data > model1 <- lme(fixed = dA ~ dCOM, data = bb1, random = ~ 1 | dCOM/dummy, > correlation= ?,method = "ML") > > Thank you very much. > > Julie. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Calculating-plotting-a-linear-regression-between-two-correlated-variables-tp4319051p4319051.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
JulieV
2012-Jan-23 14:04 UTC
[R] Calculating & plotting a linear regression between two correlated variables
Hi Josh, Thanks for your response ! Actually, I already tried to plot it with a "classical" regression and I know the relation is linear: dA = 0.765 * dCOM - 0.089 p(slope) < 0.0001 p(intercept) = 0.0003 The fact is that I can not use these results as my variables dA and dCOM are correlated (as mentioned above, Eq.1). What I need to find out is which correction I should do on my data, and how, to be able to calculte the regression p-values correctly with Linear Mixed Models. I am interested in this because I know that my species decline at different rates when my community is declining. For example, with decreasing values of dCOM, dA reaches 0 before dB. Julie -- View this message in context: http://r.789695.n4.nabble.com/Calculating-plotting-a-linear-regression-between-two-correlated-variables-tp4319051p4320764.html Sent from the R help mailing list archive at Nabble.com.