Arsen Zahray
Arsen Zahray

Reputation: 25297

How to calculate regression between 2 columns in dataframe

I'm learning how to do regression in R, and I decided to try and regress GOOG to AAPL.

Here's what I did

getSymbols("AAPL", from="2011-01-01", to="2013-01-01")
getSymbols("GOOG", from="2011-01-01", to="2013-01-01")
lmdata=data.frame(Cl(AAPL),Cl(GOOG))
res=lm(lmdata)

plot(lmdata, main="Linear regression between GOOG and AAPL")
abline(res)

The result looks like this

didn't work

Obviously, some different regression was computed, and I'm suspecting that the software computed regression of AAPL closing price to it's date

> head(lmdata)
           AAPL.Close GOOG.Close
2011-01-03     329.57   604.3510
2011-01-04     331.29   602.1210
2011-01-05     334.00   609.0711
2011-01-06     333.73   613.5011
2011-01-07     336.12   616.4411
2011-01-10     342.45   614.2110

How do I compute regression between AAPL and GOOG?

Upvotes: 0

Views: 1383

Answers (1)

m0nhawk
m0nhawk

Reputation: 24178

For lm you need to specify the formula and data in the call:

res=lm(GOOG.Close ~ AAPL.Close, data=lmdata)

Then the model and plot will be correct.

enter image description here

Upvotes: 1

Related Questions