Home > Software design >  Orthogonal Linear Regression (total least squares) fit, get RMSE and R-squared in R
Orthogonal Linear Regression (total least squares) fit, get RMSE and R-squared in R

Time:01-10

I am trying to fit a model that linearly relates two variables using R. I need to fit a Orthogonal Linear Regression (total least squares). So I'm trying to use the odregress() function of the pracma package wich performs an Orthogonal Linear Regression via PCA.

Here an example data:

x <- c(1.0, 0.6, 1.2, 1.4, 0.2, 0.7, 1.0, 1.1, 0.8, 0.5, 0.6, 0.8, 1.1, 1.3, 0.9)
y <- c(0.5, 0.3, 0.7, 1.0, 0.2, 0.7, 0.7, 0.9, 1.2, 1.1, 0.8, 0.7, 0.6, 0.5, 0.8)

I'm able to fit the model and get the coefficient using:

odr <- odregress(y, x)
c <- odr$coeff

So, the model is defined by the following equation:

print(c)
[1]  0.65145762 -0.03328271

Y = 0.65145762*X - 0.03328271

Now I need to plot the line fit, compute the RMSE and the R-squared. How can I do that?

plot(x, y)

CodePudding user response:

Here are two functions to compute the MSE and RMSE.

library(pracma)

x <- c(1.0, 0.6, 1.2, 1.4, 0.2, 0.7, 1.0, 1.1, 0.8, 0.5, 0.6, 0.8, 1.1, 1.3, 0.9)
y <- c(0.5, 0.3, 0.7, 1.0, 0.2, 0.7, 0.7, 0.9, 1.2, 1.1, 0.8, 0.7, 0.6, 0.5, 0.8)

odr <- odregress(y, x)

mse_odreg <- function(object) mean(object$resid^2)
rmse_odreg <- function(object) sqrt(mse_odreg(object))

rmse_odreg(odr)
#> [1] 0.5307982

Created on 2023-01-10 with reprex v2.0.2

  • Related