L_T
L_T

Reputation: 161

Plot of a linear regression with interactions

I need to create a plot of the linear regression of the following formula, but I have not understood which is the correct way to do it in R:

lm.velocity_vs_Velocity_response = lm(scrd$Velocity~scrd$Velocity_response*scrd$Subject)

Where scrd is my dataset which can be downloaded here: https://dl.dropbox.com/u/3288659/Velocity_vs_Velocity.csv

The dataset, corresponding to an experiment, contains 2 variables (Velocity and Velocity_response) and I want to know if there is a linear correlation between the two. Let's say that the first is the velocity of a car driven under 4 terrains conditions (snow, wood, gravel, and a material inicated with "no sound") and the second is the perceived velocity of the conductor. In the experiment the 4 conditions where repeated twice by 10 participants, who at the end of the experiment had to evaluate the perceived velocities they had whle driving in the conditions. Evaluations where performed on a visual analog scale where 0 = very slow and 10 = very fast. I have therefore 80 points in my regressions (10 participants * 2 trials * 4 estimate of the velocities). However in the dataset I decided to average the performace of the 2 trials.

The output of the formula I used to make the regression,

 summary(lm.velocity_vs_Velocity_response)

is

Residual standard error: 0.08377 on 20 degrees of freedom
Multiple R-squared:  0.91,  Adjusted R-squared: 0.8245 
F-statistic: 10.64 on 19 and 20 DF,  p-value: 1.085e-06 

from which I conclude that there is a strong correlation between the two variables (R^2 = 0.91 and p-value < 0.001)

Now, I would like to see the line fitting the linear regression on those data. How it is done in R? Which is the correct formula? Can anyone provide an example of the code in R?

The problem is that using plot I get a mess of points, and I am not able to see a linear trend.

Here I post the first rows of the dataset

Subject     Material    Velocity    Velocity_response
Subject1    no_sound    1.41        7.8
Subject1    snow        1.255       4
Subject1    gravel      1.32        5.3
Subject1    wood        1.335       5.4
Subject2    no_sound    1.435       10
Subject2    snow        1.265       1.7
Subject2    gravel      1.3         8.5
Subject2    wood        1.355       5.3

Upvotes: 4

Views: 3119

Answers (1)

Greg Snow
Greg Snow

Reputation: 49640

Your life will be much easier if you run lm like:

lm.velocity_vs_Velocity_response <- lm(Velocity~Velocity_response*Subject, data=scrd)

Then to explore the relationship and the interaction look at the Predict.Plot and TkPredict functions in the TeachingDemos package.

Upvotes: 3

Related Questions