Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
0

Pearson correlation

asked 12 years ago

Jaakko Seppälä gravatar image

Is there a method in Sage to compute Pearson correlation of a finite set? I have data points (x1,y1),,(x6,y6) and wondering if there is a shorter way to do it than just write one long expression.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 12 years ago

calc314 gravatar image

You can call R from within Sage as follows. The command r.cor does require that you give it two lists containing the coordinates from your data set. In the example below, I'll use a list comprehension in Python to do that.

data=[[1,3],[1.5,2],[5,7]]
xdata=[a[0] for a in data]
ydata=[a[1] for a in data]
r.cor(xdata,ydata)

You can also get the regression line easily.

var('a,b')
f(x)=a*x+b
ans=find_fit(data,f)
g(x)=ans[0].rhs()*x+ans[1].rhs()
plot(g(x),(x,0,10))+list_plot(data)
Preview: (hide)
link

Comments

Instead of doing two list comps, you could use `zip` and unpacking: `x, y = zip(*data)`, for example. (`r.cor(*zip(*data))` would do it in one line, but that looks a little too cute to use.)

DSM gravatar imageDSM ( 12 years ago )

Hmm. `Unable to start r`. I downloaded Sage from its WWW-site and installed that.

Jaakko Seppälä gravatar imageJaakko Seppälä ( 12 years ago )

I thought R came with the standard Sage installation.

calc314 gravatar imagecalc314 ( 12 years ago )

Alternatively, you can use `scipy`'s: `import scipy.stats` and then `scipy.stats.pearsonr(xdata, ydata) ` -- type `help(scipy.stats.pearsonr)` after the import for the details.

DSM gravatar imageDSM ( 12 years ago )

I actually like the plots from stats in scipy better than in R for use in Sage. I'm surprised by the `unable to start r` error with r.cor. I've gotten it when using R's graphics in Sage but not with the basic stats commands.

calc314 gravatar imagecalc314 ( 12 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 1,843 times

Last updated: Dec 20 '12