Ask Your Question
0

How to get coefficient of determination (r^2) in sagemath?

asked 0 years ago

anonymous user

Anonymous

I know that I can do regression with the find_fit function:

sage: R = [[1,2],[3.45,4],[6,5],[4,3]]
sage: var('a,b')
(a, b)
sage: model(x) = a*x+b
sage: find_fit(R,model)
[a == 0.56881365890949054, b == 1.445160655902004]

but it does not provide the coefficient of determination (r^2). How would i find the r^2 value in sagemath?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 0 years ago

Emmanuel Charpentier gravatar image

A lazier solution : use R's gigantic statistical libraries. Run this :

# Data matrix in Sage
M=matrix([[1,2],[3.45,4],[6,5],[4,3]])
# Build an R data frame
RData=r.data_frame(x=M.columns()[0].list(),
                   y=M.columns()[1].list())
# Do the regression in R
Rlm=r.lm(r.formula("y~x"),
         data=RData)
# Compute the relevant indicators
Rsum=r.summary(Rlm)
# Regression coefficients
b, a = Rlm._sage_()['DATA']['coefficients']['DATA']
# Coefficient of determination
R_squared=Rsum._sage_()['DATA']['r.squared']

Then :

sage: R_squared
0.821935737833981
sage: a
0.568813659400679
sage: b
1.44516065541505

Yes, in that simple case, that's heavier than @tolga's solution. But its principles are applicable to any problem having a solution in R, not just the coefficient of determination of a single-regressor linear regresssion.

HTH,

Preview: (hide)
link
1

answered 0 years ago

tolga gravatar image

Using numpy arrays, I wrote a function for you (I used Google Gemini for TSS and RSS):

def coeff_of_det(R,coeffs):
    import numpy as np
    # Extract x and y values
    x_values = np.array([point[0] for point in R])
    y_true = np.array([point[1] for point in R])

    # Predict y values
    y_pred = [model.subs(coeffs)(x_val) for x_val in x_values]

    # R**2
    y_mean = n(sum((y_true)) / len(y_true))
    TSS = sum((y_true - y_mean)**2)
    RSS = sum((y_true - y_pred)**2)
    r_squared = 1 - (RSS / TSS)
    return r_squared


R = [[1, 2], [3.45, 4], [6, 5], [4, 3]]
var('a, b')
model(x) = a * x + b
coeffs = find_fit(R, model)

r_sq=coeff_of_det(R,coeffs)

print(r_sq)
Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 0 years ago

Seen: 96 times

Last updated: May 23