# Curve Fitting Using Linear Regression

by admin in Curve Fitting , Interpolation , Math, Statistics, and Optimization , MATLAB Family on April 26, 2019This code fits a Curve using the Linear Regression method, that based on minimizing the sum of the squares of the residuals between the measured y and the y calculated.

### Code Outputs:

- Chart Presents the Given Points and the Resultant Linear Curve
- The Fitted Y Values of any inserted X Points, Vector or Scalar.
- Standard Error
- Coefficient of Determination

The Fited Values at Xi are: 4.0687

Coefficient of Determination r = 0.96182. Standard Error Syx = 0.6685

### Input Requirements:

- X Points
- Y Points

### About The Method:

**linear regression** is a linear approach to modelling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables). The case of one explanatory variable is called simple linear regression. For more than one explanatory variable, the process is called **multiple linear regression**. This term is distinct from multivariate linear regression, where multiple correlated dependent variables are predicted, rather than a single scalar variable.

In linear regression, the relationships are modeled using linear predictor functions whose unknown model parameters are estimated from the data. Such models are called linear models. Most commonly, the conditional mean of the response given the values of the explanatory variables (or predictors) is assumed to be an affine function of those values; less commonly, the conditional median or some other quantile is used. Like all forms of regression analysis, linear regression focuses on the conditional probability distribution of the response given the values of the predictors, rather than on the joint probability distribution of all of these variables, which is the domain of multivariate analysis.

Linear regression was the first type of regression analysis to be studied rigorously, and to be used extensively in practical applications. This is because models which depend linearly on their unknown parameters are easier to fit than models which are non-linearly related to their parameters and because the statistical properties of the resulting estimators are easier to determine.

Linear regression has many practical uses. Most applications fall into one of the following two broad categories:

- If the goal is prediction, or forecasting, or error reduction, linear regression can be used to fit a predictive model to an observed data set of values of the response and explanatory variables. After developing such a model, if additional values of the explanatory variables are collected without an accompanying response value, the fitted model can be used to make a prediction of the response.
- If the goal is to explain variation in the response variable that can be attributed to variation in the explanatory variables, linear regression analysis can be applied to quantify the strength of the relationship between the response and the explanatory variables, and in particular to determine whether some explanatory variables may have no linear relationship with the response at all, or to identify which subsets of explanatory variables may contain redundant information about the response.

Linear regression models are often fitted using the least squares approach, but they may also be fitted in other ways, such as by minimizing the ‘lack of fit’ in some other norm (as with least absolute deviations regression), or by minimizing a penalized version of the least squares cost function as in ridge regression (*L*^{2}-norm penalty) and lasso (*L*^{1}-norm penalty). Conversely, the least squares approach can be used to fit models that are not linear models. Thus, although the terms ‘least squares’ and ‘linear model’ are closely linked, they are not synonymous.

### Regretion Method:

Given a data set **{yi, xi1, …, xip}^n,i=1** of ** n** statistical units, a linear regression model assumes that the relationship between the dependent variable

*y*and the

*p*-vector of regressors

**x**is linear. This relationship is modeled through a

*disturbance term*or

*error variable*

**— an unobserved random variable that adds ‘noise’ to the linear relationship between the dependent variable and regressors. Thus the model takes the form**

*ε*where ** ^{T}** denotes the transpose, so that

**x**

_{i}

^{T}*is the inner product between vectors*

**β****x**

_{i}and

*.*

**β**Often these ** n** equations are stacked together and written in matrix notation as

where

Some remarks on notation and terminology:

**y**is a vector of observed values**yi (i = 1, …, n)**of the variable called the*regressand*,*endogenous variable*,*response variable*,*measured variable*,*criterion variable*, or*dependent variable*. This variable is also sometimes known as the*predicted variable*, but this should not be confused with*predicted values*, which are denoted**y^**. The decision as to which variable in a data set is modeled as the dependent variable and which are modeled as the independent variables may be based on a presumption that the value of one of the variables is caused by, or directly influenced by the other variables. Alternatively, there may be an operational reason to model one of the variables in terms of the others, in which case there need be no presumption of causality.may be seen as a matrix of row-vectors**X****xi**or of*n*-dimensional column-vectors**Xj**, which are known as*regressors*,*exogenous variables*,*explanatory variables*,*covariates*,*input variables*,*predictor variables*, or*independent variables*(not to be confused with the concept of independent random variables). The matrix**X**is sometimes called the design matrix.- Usually a constant is included as one of the regressors. In particular,
**Xi0 = 1**for**i = 1, …, n.**The corresponding element ofis called the**β***intercept*. Many statistical inference procedures for linear models require an intercept to be present, so it is often included even if theoretical considerations suggest that its value should be zero. - Sometimes one of the regressors can be a non-linear function of another regressor or of the data, as in polynomial regression and segmented regression. The model remains linear as long as it is linear in the parameter vector
.**β** - The values
may be viewed as either observed values of random variables*x*_{ij}or as fixed values chosen prior to observing the dependent variable. Both interpretations may be appropriate in different cases, and they generally lead to the same estimation procedures; however different approaches to asymptotic analysis are used in these two situations.*X*_{j}

- Usually a constant is included as one of the regressors. In particular,
is a**β****(P + 1)**-dimensional*parameter vector*, whereis the intercept term (if one is included in the model—otherwise**β0**is**β***p*-dimensional). Its elements are known as*effects*or*regression coefficients*(although the latter term is sometimes reserved for the*estimated*effects). Statistical estimation and inference in linear regression focuses on. The elements of this parameter vector are interpreted as the partial derivatives of the dependent variable with respect to the various independent variables.**β**is a vector of values*ε*. This part of the model is called the*ε*_{i}*error term*,*disturbance term*, or sometimes*noise*(in contrast with the ‘signal’ provided by the rest of the model). This variable captures all other factors which influence the dependent variable*y*other than the regressors**x**. The relationship between the error term and the regressors, for example their correlation, is a crucial consideration in formulating a linear regression model, as it will determine the appropriate estimation method.

**Example**. Consider a situation where a small ball is being tossed up in the air and then we measure its heights of ascent ** h_{i} **at various moments in time

**. Physics tells us that, ignoring the drag, the relationship can be modeled as**

*t*_{i}where ** β_{1} **determines the initial velocity of the ball,

**is proportional to the standard gravity, and**

*β*_{2}**is due to measurement errors. Linear regression can be used to estimate the values of**

*ε*_{i}**and**

*β*_{1}**from the measured data. This model is non-linear in the time variable, but it is linear in the parameters**

*β*_{2}**and**

*β*_{1}**; if we take regressors**

*β*_{2}**x**the model takes on the standard form

_{i}= (*x*_{i1},*x*_{i2}) = (*t*_{i},*t*_{i}^{2}),### Interpretation:

A fitted linear regression model can be used to identify the relationship between a single predictor variable ** x_{j} **and the response variable

**when all the other predictor variables in the model are ‘held fixed’. Specifically, the interpretation of**

*y***is the expected change in**

*β*_{j}*y*for a one-unit change in

*x*_{j }when the other covariates are held fixed—that is, the expected value of the partial derivative of

*with respect to*

**y****. This is sometimes called the**

*x*_{j}*unique effect*of

**on**

*x*_{j}**. In contrast, the**

*y**marginal effect*of

**on**

*x*_{j}*y*can be assessed using a correlation coefficient or simple linear regression model relating only

**to**

*x*_{j}**; this effect is the total derivative of**

*y***with respect to**

*y***.**

*x*_{j}Care must be taken when interpreting regression results, as some of the regressors may not allow for marginal changes (such as dummy variables, or the intercept term), while others cannot be held fixed (recall the example from the introduction: it would be impossible to ‘hold ** t_{i} **fixed’ and at the same time change the value of

**).**

*t*_{i}^{2}It is possible that the unique effect can be nearly zero even when the marginal effect is large. This may imply that some other covariate captures all the information in ** x_{j}**, so that once that variable is in the model, there is no contribution of

**to the variation in**

*x*_{j}**. Conversely, the unique effect of**

*y*

*x*_{j }can be large while its marginal effect is nearly zero. This would happen if the other covariates explained a great deal of the variation of

**, but they mainly explain variation in a way that is complementary to what is captured by**

*y***. In this case, including the other variables in the model reduces the part of the variability of**

*x*_{j}**that is unrelated to**

*y***, thereby strengthening the apparent relationship with**

*x*_{j}**.**

*x*_{j}The meaning of the expression ‘held fixed’ may depend on how the values of the predictor variables arise. If the experimenter directly sets the values of the predictor variables according to a study design, the comparisons of interest may literally correspond to comparisons among units whose predictor variables have been ‘held fixed’ by the experimenter. Alternatively, the expression ‘held fixed’ can refer to a selection that takes place in the context of data analysis. In this case, we ‘hold a variable fixed’ by restricting our attention to the subsets of the data that happen to have a common value for the given predictor variable. This is the only interpretation of ‘held fixed’ that can be used in an observational study.

The notion of a ‘unique effect’ is appealing when studying a complex system where multiple interrelated components influence the response variable. In some cases, it can literally be interpreted as the causal effect of an intervention that is linked to the value of a predictor variable. However, it has been argued that in many cases multiple regression analysis fails to clarify the relationships between the predictor variables and the response variable when the predictors are correlated with each other and are not assigned following a study design. Commonality analysis may be helpful in disentangling the shared and unique impacts of correlated independent variables.

### References:

[1] David A. Freedman (2009). *Statistical Models: Theory and Practice*. Cambridge University Press. p. 26. A simple regression equation has on the right hand side an intercept and an explanatory variable with a slope coefficient. A multiple regression equation has two or more explanatory variables on the right hand side, each with its own slope coefficient

[2] Rencher, Alvin C.; Christensen, William F. (2012), ‘Chapter 10, Multivariate regression – Section 10.1, Introduction’, *Methods of Multivariate Analysis*, Wiley Series in Probability and Statistics, **709** (3rd ed.), John Wiley & Sons, p. 19, ISBN 9781118391679.

[3] Hilary L. Seal (1967). ‘The historical development of the Gauss linear model’. *Biometrika*. **54** (1/2): 1–24. doi:10.1093/biomet/54.1-2.1. JSTOR 2333849.

[4] Yan, Xin (2009), *Linear Regression Analysis: Theory and Computing*, World Scientific, pp. 1–2, ISBN 9789812834119, Regression analysis … is probably one of the oldest topics in mathematical statistics dating back to about two hundred years ago. The earliest form of the linear regression was the least squares method, which was published by Legendre in 1805, and by Gauss in 1809 … Legendre and Gauss both applied the method to the problem of determining, from astronomical observations, the orbits of bodies about the sun.

Share Now!