OLS.reg | R Documentation |
Fits a function f(x_1, x_2, \dots, x_{n-1}, \vec{v} )
to a multivariate dataset \left(x_1, x_2, \dots , x_{n-1}, y \right)
.
The vector \vec{v}
contains the parameters that are to be extracted by the fitting procedure.
Uses OLS regression methodology.
OLS.reg(model, dat, guess, algorithm = 'Nelder-Mead', Delta = 10^-6)
OLS.reg(model, dat, guess, algorithm)
OLS.reg(model, dat, guess, Delta)
OLS.reg(model, dat, guess)
model |
Has to be a function,
|
dat |
Has to be the dataframe with |
guess |
Has to be a numeric vector. |
algorithm |
Denotes the optimization algorithm. |
Delta |
Has to be a numeric scalar, |
With f(x_1, x_2, \dots, x_{n-1}, \vec{v} )
, make sure that the dat
dataframe has columns x_1, x_2, \dots , x_{n-1}, y
in the SAME ORDER.
Uses the simple squared error and mean squared error loss and cost functions.
l_i(\vec{v}) = \{ y_i - f(x_{i,1}, x_{i,2}, \dots x_{i, n-1}, \vec{v}) \}^2
C(\vec{v})= \frac{1}{N}{{\sum_i l_i(\vec{v})}}
The reurned value is a 6 element list
par
: The optimized/extracted parameters for the vector \vec{v}
value
: The value of the cost function C(\vec{v})
at the optimized value of \vec{v}
.
counts
: This a 2-element vector. 1st elem: tells us the no. of times the function was evaluated, 2nd elem: gradient at that point. See optim
convergence
: Is an integer value, 0 implies succesful convergence, 1 implies maximum no. of iterations reached prior to convergence, 10 implies degenerate problem detected. See optim.
message
: helps diagnose convergence issues. See optim.
err
: The residuals, e_i
, after the fitting process, calculated at the optimized value of \vec{v}
e_i = y_i - f(x_{i,1}, x_{i,2}, \dots, x_{i,n-1}, \vec{v} )
Chitran Ghosal
#Build the dataframe for dat
X1 <- sort(rnorm(500))
X2 <- sort(rnorm(500))
Y <- 110*X1 + 120*X2
Y <- Y + rnorm(n = length(X1), mean = 0, sd = 10)
plot(X1, Y)
plot(X2, Y)
df <- data.frame(X1, X2, Y)
#Build the function for the model
lin.fun <- function(X1, X2, v){
Y <- v[1]*X1 + v[2]*X2
return(Y)
}
lst <- OLS.reg(model = lin.fun, dat = df, guess = c(50, 50), algorithm = 'BFGS')
library(StatsChitran)
plot(X1, Y)
lines(X1, lin.fun(X1, X2, v=lst$par), col='red', lwd=3)
plot(X2, Y)
lines(X2, lin.fun(X1, X2, v=lst$par), col='red', lwd=3)
lst$par
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.