R Linear Regression
[ R Language Examples](#)\\n\\nIn statistics, Linear Regression is a regression analysis method that uses the least squares function to model the relationship between one or more independent variables and a dependent variable.\\n\\nSimply put, it is a statistical analysis method used to determine the quantitative relationship between two or more variables that depend on each other.\\n\\nIn regression analysis, when there is only one independent variable and one dependent variable, and the relationship between them can be approximated by a straight line, this type of regression analysis is called simple linear regression analysis. If the regression analysis includes two or more independent variables, and there is a linear relationship between the dependent variable and the independent variables, it is called multiple linear regression analysis.\\n\\nThe mathematical equation for simple linear regression analysis:\\n\\ny = ax + b\\n\\n* **y** is the value of the dependent variable.\\n\\n* **x** is the value of the independent variable.\\n\\n* **a** and **b** are the parameters of the simple linear regression equation.\\n\\nNext, we can create a prediction model for human height and weight:\\n\\n* 1γCollect sample data: height and weight.\\n* 2γUse the lm() function to create a relationship model.\\n* 3γFind the coefficients from the created model and create a mathematical equation.\\n* 4γGet the summary of the relationship model to understand the average error, i.e., the residuals (the difference between estimated and actual values).\\n* 5γUse the predict() function to predict a person's weight.\\n\\n### Prepare data\\n\\nHere is the height and weight data:\\n\\n# Height, unit cm 151, 174, 138, 186, 128, 136, 179, 163, 152, 131# Weight, unit kg 63, 81, 56, 91, 47, 57, 76, 72, 62, 48\\n### lm() functions\\n\\nIn R, you can perform linear regression through the lm() function.\\n\\nThe lm() function is used to create a relationship model between independent and dependent variables.\\n\\nThe lm() function syntax is as follows:\\n\\nlm(formula,data)\\nParameter description:\\n\\n* formula - A symbolic formula representing the relationship between x and y.\\n* data - The data to apply.\\n\\nCreate a relationship model and get the coefficients:\\n\\n## Examples\\n\\n# Sample data\\n\\n x <-c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)\\n\\n y <-c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)\\n\\n# Pass to lm() functions\\n\\n relation <-lm(y~x)\\n\\nprint(relation)\\n\\nExecuting the above code produces the following output:\\n\\nCall: lm(formula = y ~ x)Coefficients:(Intercept) x -38.4551 0.6746 \\nUse the **summary()** function to get the summary of the relationship model:\\n\\n## Examples\\n\\nx <-c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)\\n\\n y <-c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)\\n\\n# Pass to lm() functions\\n\\n relation |t|) (Intercept) -38.45509 8.04901 -4.778 0.00139 ** x 0.67461 0.05191 12.997 1.16e-06 ***---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1Residual standard error: 3.253 on 8 degrees of freedom Multiple R-squared: 0.9548, Adjusted R-squared: 0.9491 F-statistic: 168.9 on 1 and 8 DF, p-value: 1.164e-06\\n### predict() functions\\n\\nThe predict() function is used to predict values based on the model we established.\\n\\nThe predict() function syntax is as follows:\\n\\npredict(object, newdata)\\nParameter description:\\n\\n* object - The formula created by the lm() function.\\n* newdata - The values to predict.\\n\\nIn the following example, we predict a new weight value:\\n\\n## Examples\\n\\n# Sample data\\n\\n x <-c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)\\n\\n y <-c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)\\n\\n# Pass to lm() functions\\n\\n relation <-lm(y~x)\\n\\n# Predict the weight for a height of 170cm\\n\\n a <-data.frame(x =170)\\n\\n result <-predict(relation,a)\\n\\nprint(result)\\n\\nExecuting the above code produces the following output:\\n\\n1 76.22869 \\nWe can also generate a chart:\\n\\n## Examples\\n\\n# Sample data\\n\\n x <-c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)\\n\\n y <-c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)\\n\\n relation <-lm(y~x)\\n\\n# Generate PNG image\\n\\npng(file="linearregression.png")\\n\\n# Generate chart\\n\\nplot(y,x,col="blue",main ="Height & Weight Regression",\\n\\nabline(lm(x~y)),cex =1.3,pch =16,xlab ="Weight in Kg",ylab ="Height in cm")\\n\\nThe chart is as follows:\\n\\n!(#)\\n\\n[![Image 3: R Language Examples](
YouTip