当前位置:网站首页>R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model

R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model

2022-06-11 21:52:00 Deep bamboo breeze

Be careful : Classic models and R Language codes have been learned in other books , Don't take too many notes .

1 Linear regression

1.1 Establish and draw map analysis data

library(e1071)
library(plyr)
library(ggplot2)

In the book , First, the missing value is judged . Then the graph analysis is carried out .

Use boxplot() Package drawing box line diagram , Use ggplot2 Package drawing histogram , Use density() Calculate the density value of the data and use plot() Function drawing , Finally, I use ggplot2 Draw a scatter plot .

1.2 Build a linear model

1. The simple way to use it is to ggplot2 Used in the bag stat_smooth function , Such as stat_smooth(method="lm",col="red",size=1)

2. in addition , You can directly build a linear model and use summary Function to get details .

model<-lm(x~y+z,data=newdata)

1.3 Perform graphic diagnostics

  • The residual diagram is used to show the difference between the predicted results of the model and the real results .
  • Use QQ Fig. check whether the data meet the assumption of normal distribution .
  • The proportional position map is used to show the relationship between the normalized residuals and the predicted values .
  • Residuals Leverage Graphs are used to measure the importance of data .

1.4 prediction model

In general Data sets account for 70%, Test set accounts for 30%. Because the sampling code is classic , So record .

set.seed(123)
trainingrowindex<-sample(1:nrow(newdata),0.7*nrow(newdata))
trainingData<-newdata[trainingrowindex, ]
testData<-newdata[-trainingrowindex, ]

The model code

mod<-lm(x~y+z, data=traindata)
predict<-predict(mod,testdata)
summary(mod)

1.5 summary

  Linear regression is the most commonly used statistical model in regression models , Describes the linear relationship between data . To make the model more stable , Cross validation can be used for modeling , And then get more stable model results .

2 Logical regression

原网站

版权声明
本文为[Deep bamboo breeze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112138256698.html