# How to analyze relationship between data pairs

Please change the title if it's not accurate, I might be lacking terminology.

I have two variables ($x,y$) which are directly correlated by pair. For example I have sampled 50 trees in my study area and for each tree I have tree height and annual precipitation at that location. This is what I mean by they are organized by pair. So I plot $x$ vs $y$ and I would like to know if there is any correlation between the two; does annual precipitation predict tree height, or the other way around?

I have used R to test if there is any linear correlation with the function lm(y~x) and have $p<.05$ and $R^2=0.6$, but some of my data seem as if they might be better fitted with a non-linear curve. What are some other types of statistical analysis I should use for such a problem?

• You ask, "does annual precipitation predict tree height, or the other way around?" Do you want to learn the causal relationship between the two variables, or just predict one from the other? – Lizzie Silver Oct 20 '14 at 23:56
• What does the graph plot(x,y) look like? Can you post it here? – rnso Oct 21 '14 at 2:44
• @rnso I have different graphs for several species, some look somewhat linear, others aren't linear at all. postimg.org/image/ny2eoidmb postimg.org/image/n9tk5kewj – Herman Toothrot Oct 21 '14 at 11:04
• @LizzieSilver that's a very good point, at this stage I am just exploring any relationship between the two and then I will leave the biological interpretation to someone else. – Herman Toothrot Oct 21 '14 at 11:09
• You have separate and long-term precipitation measurements by every tree? That is very impressive data. – Nick Cox Oct 21 '14 at 11:47

Rank correlation coefficients such as Kendall's or Spearman's can be useful to measure the strength of non-linear monotonic relationships, i.e. those that are increasing or decreasing, but not e.g. U-shaped. Calculating confidence intervals for their population values is often considered more useful than simply testing whether they're equal to zero.

If you want to predict tree height from precipitation (or vice versa) there are many approaches (see @Lizzie's links). The most basic extension of simple linear regression is simply to represent the predictor $x$ as a polynomial & estimate a coefficient $\beta_k$ for each term $k$: $$\operatorname{E}Y =\beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + ...$$ where $\operatorname{E}Y$ is the predicted response. With just one predictor you might use F-tests to see how many terms are necessary (too many compared to the number of observations & you'll over-fit the model to the sample data).

• Thanks for the suggestion, I am not sure how I can have a polynomial when I only have one variable that I am trying to correlate with what i am trying to predict. – Herman Toothrot Oct 24 '14 at 17:00
• The one variable is $x$ above; you can represent it with more than one term - linear, quadratic, cubic &c. - in the model equation. I'd suggest reading an introductory textbook on regression - Faraway (2002), Practical Regression & ANOVA Using R might be useful, as you're using R. – Scortchi - Reinstate Monica Oct 25 '14 at 20:18

Kernel regression and smoothing splines will both fit non-linear regression functions very nicely. There's a longer list of methods, with examples, in this R-bloggers post by Wesley.

To visualize nonlinear data, try:

library(ggplot2)
ggplot(rndf, aes(x=vnum1, y=vnum2, group=vbin1, color=vbin1))+stat_smooth()


Where vnum1 and vnum2 are your x and y and vbin1 is the species or other grouping column.

Different options can be set in stat_smooth() function.

The shaded areas indicate 95% CI. Overlapping areas would indicate non-significant difference.

• thanks I have tried that but how do I determine if there is anything statistially significant? – Herman Toothrot Oct 21 '14 at 13:12
• I have added a note regarding visualization of significant difference. I am not sure about calculations. – rnso Oct 21 '14 at 14:18
• You may be overinterpreting the overlapping areas: after all, overlapping 95% CIs can occur even among highly significant differences. But why are you recommending these plots? How does this set of plots answer the stated question? – whuber Oct 21 '14 at 17:55
• @ whuber: I am suggesting these plots only for an overview of data which may be non-linear. I thought if 95% CI are overlapping, the p value will be >0.05 i.e. not significant difference. But my experience is nowhere compared to yours. Please suggest me some links to refer to or explain briefly. – rnso Oct 22 '14 at 0:53
• There seem to be a lot of posts on our site about overlapping confidence intervals: stats.stackexchange.com/…. – whuber Oct 24 '14 at 15:45