Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use R language Linear regression

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use R language linear regression". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "how to use R language linear regression"!

I. R installation

R for Mac OS

RStudio

Second, the basis of linear regression

Eg1:

From our learning experience, we can see that there is indeed a certain relationship between physics and mathematics. But in addition, there are other factors that affect physics performance, such as whether you like physics or not, using the time spent on physics study, and so on.

Eg2:

The relationship between commodity sales income and advertising expenditure, and there is a close relationship between commodity sales income and advertising expenditure. But there are many other factors.

Eg3:

The relationship between grain yield and fertilizer application. In a certain range, the greater the amount of fertilizer application, the higher the grain yield, but the amount of fertilizer application is not the only factor affecting grain yield.

Age 23273941454950 fat 9.517.821.225.927.526.328.2 Age 53545657586061 fat 29.630.231.430.833.535.234.6

What is the relationship between body fat content and age?

First of all, let's do a scatter chart. The x-axis represents age and the y-axis represents fat. (make your own scatter chart,)

From the scatter plot, you can see that they are scattered from the lower left corner to the upper right corner. For this correlation between the two variables, we call it positive correlation. Other variables, such as the weight of the car and the average distance the car travels per lL of gasoline consumed, are called negative correlations.

As can be seen from the scatter plot, these points are roughly distributed near a straight line passing through the center of the scatter plot. If the center distribution of the scatter graph is approximately near a straight line as a whole, we say that there is a linear correlation between the two variables, which is called the regression line (regression line). If we can find the equation of this regression line (regression equation), then we will have a clearer understanding of the relationship between age and fat.

Least square method

In fact, the key to finding the regression equation is how to use mathematical methods to describe "on the whole, the distance from each point to this straight line is the smallest."

Suppose the line is y = bx + a

Note:

(x1) (x2). (xi,yi).. One by one. The distance to the line can be calculated by the distance from the point to the line, but this is more complicated.

So the first simplification = = > point (xi,yi), the distance up or down to the line yi-y = yi-(bxi + a)

But yi-y is positive and negative, so the second time we simplify it, it's squared.

Q = (y1-bx1-a) ^ 2 + (y2-bx2-a) ^ 2 +. + (yi-bxi-a) ^ 2 +.

As long as the minimum an and b of Q are calculated, the regression equation can be obtained.

A can be calculated using partial derivatives to get a _ r _ b

A =.

B =.

B is the slope of the regression line, and an is the intercept.

The strength of the correlation

We know that the relationship between not all two variables is linear, how to judge whether linear regression can be used?

In statistics, the correlation coefficient r is used to measure the linear relationship between the two variables.

R =.

According to statistics, if r sd (y_weight) = > calculated standard deviation [1] 2.077498 > cor (x determinagereflexive weight) = > calculated correlation [1] 0.9075655 > plot (x_age) Y_weight) = > drawing scatter plot > cor_xy = lm (y_weight~x_age + 1) = > setting fitting curve equation > plot (cor_xy) = > drawing regression line > coef (cor_xy) = > calculating the slope and intercept of regression line (Intercept) x_age 4.3596206 0.4655827 > z = data.frmae (x_age=13) > predict (cor_xy) Z) > Let's predict the weight of a 13-month-old baby. Thank you for reading. The above is the content of "how to use R language linear regression". After the study of this article, I believe you have a deeper understanding of how to use R language linear regression, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report