Tried to calculate the R-squared value for the exponential and power trendline type using the following equation.
R² = ∑(ȳ - ŷ)² / ∑(y - ȳ)²
Here ȳ is the mean value of y and ŷ is the regression equation, here ŷ for
exponential is y = ae^xb
and for power is y = ax^b
Here a = intercept and b = slope.
I have implemented the following code snippet logic to achieve the R squared value
List<int> xValue = <int>[1,2,3,4,5];
List<int> yValue = <int>[2,4,5,4,5];
int sumOfX = 0,sumOfY = 0,sumOfXY = 0, sumOfXSquare = 0, sumOfYSquare = 0;
double slope = 0.5204, intercept = 2.3133;
for(int i = 0; i < xValue.length; i++){
sumOfX += xValue[i];
sumOfY += yValue[i];
sumOfXY += xValue[i] * yValue[i];
sumOfXSquare += math.pow(xValue[i], 2).toInt();
sumOfYSquare += math.pow(yValue[i], 2).toInt();
}
double yMean = sumOfY/yValue.length;
// value of ∑(y - ȳ)²
double ssTol = 0;
for(int j = 0; j < yValue.length; j++){
ssTol += math.pow((yValue[j] - yMean),2);
}
// value of ∑(ȳ - ŷ)²
double ssReg = 0;
for(int k = 0; k < yValue.length; k++){
ssReg += math.pow(((intercept * math.pow(xValue[k], slope)) -yMean), 2).toDouble();
}
print(ssReg/ssTol);
I want the R-squared value like below.

Can anyone please help me with this?