# Linear regression on several trials of experiment

Sorry about asking such a basic question.

Assuming I have data like this

x Trial1 Trial2 Trial3 1 1.0 2.0 3.0 2 1.1 2.1 3.1 3 1.2 2.2 3.2

How exaclty to I regress my predictor variable x onto my data. I was thinking naively that I could just take the average of the data trials and get something like

x y 1 1.1 2 2.1 3 3.1 for which I could simply use model <-lm(x ~ y). But I think I would introduce a bias by using the average. I was thinking instead to to create ordered pairs like (1,1.1), (1,2.0),...(3,2.2),(3,3.2). And use that for the regression but I'm not quite sure how to do this in a clean way in R. I was going to use data slicing and cbind a bunch of times, but I'm sure there is something better.

An example of what I'm getting at

The crux of my question is: how to do a regression model when you have data from mutiple runs of the same experiment. For example if I was looking at the temperature along a really thin metal wire (so its essentially 1-d), and then I mark off lenghts on this wire called x=0, x=1, x=2, ... x=total length. Then I take 10 temperature measuresments for each value of x. How do I create a regression model, which gives Temperarute as a function of length, i.e. T(x), given that I have 10 T values for each value of x?

• Either your terminology is wrong or your model is written wrongly. Model formulas go y ~ x for response y and predictor x. – Glen_b -Reinstate Monica Jun 27 '14 at 8:58
• You have what's called "repeated measures." There's a good long explanation in the answer here: stats.stackexchange.com/questions/35590/… – shadowtalker Jun 27 '14 at 9:04
• @Glen_b, Sorry that's just a typo. – user49105 Jun 27 '14 at 15:30
• Can you please edit your question to clarify your actual meaning? – Glen_b -Reinstate Monica Jun 27 '14 at 16:01
• @Glen_b, The crux of my question is: how to do a regression model when you have data from mutiple runs of the same experiment. For example if I was looking at the temperature along a really thin metal wire (so its essentially 1-d), and then I mark off lenghts on this wire called x=0, x=1, x=2, ... x=total length. Then I take 10 temperature measuresments for each value of x. How do I create a regression model, which gives Temperarute as a function of length, i.e. T(x), given that I have 10 T values for each value of x? – user49105 Jun 27 '14 at 16:24

This is actually exactly the kind of data you're supposed to have for regression. Repeat observations at each value of X just means you have more information for that value of X. Just regress Temp on X, then you're modeling $\mathbb{E}[Y|X] = \alpha + \beta\cdot X$.
What you should do in this case, however, is check for clustering in your errors. The easiest way to do this is to run both a standard linear regression and a cluster-robust linear regression (Google how to implement it for your program of choice). If the coefficients are different, use the cluster-robust version. Basically what this is doing is saying "I suspect that observations taken from the same wire are not independent of each other," and if the suspicion here is correct then the estimates of standard errors (and therefore your p-values) will be inconsistent (that is, they do not converge to their "true" values as $n\rightarrow\infty$). This page has a bit more on how to interpret any differences between cluster-robust and classical ("homoskedastic") standard errors. It uses Stata as an example but the point is general.
However, that still assumes that the relationship (the value of $\alpha,\beta$) is the same for every wire. If you don't think that's a good assumption, or you're trying to test that assumption, this is the classic use case for a hierarchical/multilevel model.