欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語言在散點(diǎn)圖中添加lm線性回歸公式的問題

 更新時(shí)間:2022年09月07日 14:45:46   作者:我是大南瓜  
這篇文章主要介紹了R語言在散點(diǎn)圖中添加lm線性回歸公式的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1. 簡單的線性回歸

函數(shù)自帶的例子(R 中鍵入?lm),lm(y ~ x)回歸y=kx + b, lm( y ~ x -1 )省略b,不對(duì)截距進(jìn)行估計(jì)

require(graphics)

## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept

anova(lm.D9)
summary(lm.D90)

opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(lm.D9, las = 1)      # Residuals, Fitted, ...
par(opar)

使用R中自帶的mtcars數(shù)據(jù),可以得到截距和斜率,也可以得到解釋率R-square:

require(ggplot2)
library(dplyr) #加載dplyr包
library(ggpmisc) #加載ggpmisc包
library(ggpubr)
require(gridExtra)
model=lm(mtcars$wt ~ mtcars$mpg)
model
## 輸出:
Call:
lm(formula = mtcars$wt ~ mtcars$mpg)

Coefficients:
(Intercept)   mtcars$mpg  
      6.047       -0.141
      ```
```handlebars
summary(model)

## 輸出:
Call:
lm(formula = mtcars$wt ~ mtcars$mpg)

Residuals:
   Min     1Q Median     3Q    Max 
-0.652 -0.349 -0.138  0.319  1.368 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   6.0473     0.3087   19.59  < 2e-16 ***
mtcars$mpg   -0.1409     0.0147   -9.56  1.3e-10 ***
---
Signif. codes:  0 ‘***' 0.001 ‘**' 0.01 ‘*' 0.05 ‘.' 0.1 ‘ ' 1

Residual standard error: 0.494 on 30 degrees of freedom
Multiple R-squared:  0.753,	Adjusted R-squared:  0.745 
F-statistic: 91.4 on 1 and 30 DF,  p-value: 1.29e-10

提取回歸R-square值:

通過summary提?。?
## 上面的例子


## mtcars例子
model=lm(mtcars$wt ~ mtcars$mpg)
res=summary(model)
str(res) 
## 提取各個(gè)值:
res$r.squared
res$coefficients
res$adj.r.squared  ## df 矯正后的結(jié)果
res$coefficients[1,1]
res$coefficients[2,1]

使用默認(rèn)的plot繪制回歸散點(diǎn):

plot(mtcars$mpg, mtcars$wt, pch=20,cex=2)
abline(model,col="red",lwd=2)

計(jì)算Confidence interval(95%):

test=mtcars[c("mpg","wt")]
head(test)
colnames(test)=c("x","y")
model = lm(y ~ x, test)

test$predicted = predict(
  object = model,
  newdata = test)

test$CI = predict(
  object = model,
  newdata = test,
  se.fit = TRUE
)$se.fit * qt(1 - (1-0.95)/2, nrow(test))

test$predicted = predict(
  object = model,
  newdata = test)

test$CI_u=test$predicted+test$CI
test$CI_l=test$predicted-test$CI
plot(mtcars$mpg, mtcars$wt, pch=20,cex=1) ##  have replicated x values
abline(model,col="red",lwd=2)
lines(x=test$x,y=test$CI_u,col="blue")
lines(x=test$x,y=test$CI_l,col="blue")

上面的圖藍(lán)線有點(diǎn)奇怪,簡單繪制最初的plot:

plot(mtcars$mpg, mtcars$wt, pch=20,cex=1,type="b") ##  have replicated x values

實(shí)際上面的計(jì)算方法沒問題,但是數(shù)據(jù)不合適,因?yàn)閿?shù)據(jù)x含有重復(fù)值,所以要考慮這個(gè)。

2. 使用ggplot2展示

ggplot2例子:

p <- ggplot(df, aes(x=yreal, y=ypred)) +
  geom_point(color = "grey20",size = 1, alpha = 0.8)
#回歸線
#添加回歸曲線
p2 <- p + geom_smooth(formula = y ~ x, color = "red",
                      fill = "blue", method = "lm",se = T, level=0.95) +
  theme_bw() +
  stat_poly_eq(
    aes(label = paste(..eq.label.., ..adj.rr.label.., sep = '~~~')),
    formula = y ~ x,  parse = TRUE,color="blue",
    size = 5, #公式字體大小
    label.x = 0.05,  #位置 ,0-1之間的比例
    label.y = 0.95) + 
  labs(title="test",x="Real Value (Huang Huaihai 1777)" , y="Predicted Value (Correlation: 0.5029)")
p2

ggplot版本的手動(dòng)計(jì)算:

require(ggplot2)
library(dplyr) #加載dplyr包
library(ggpmisc) #加載ggpmisc包
library(ggpubr)
require(gridExtra)
ggplot(data=df, aes(x=yreal, y=ypred)) +
  geom_smooth(formula = y ~ x, color = "blue",
              fill = "grey10", method = "lm")  +
  geom_point() +
  stat_regline_equation(label.x=0.1, label.y=-1.5) +
  stat_cor(aes(label=..rr.label..), label.x=0.1, label.y=-2)

test=df
head(test)
colnames(test)=c("x","y")
model = lm(y ~ x, test)
test$predicted = predict(
  object = model,
  newdata = test)

test$CI = predict(
  object = model,
  newdata = test,
  se.fit = TRUE
)$se.fit * qt(1 - (1-0.95)/2, nrow(test))

ggplot(test) +
  aes(x = x, y = y) +
  geom_point(size = 1,colour="grey40") +
  geom_smooth(formula =y ~ x,method = "lm",  fullrange = TRUE, color = "black") +
  geom_line(aes(y = predicted + CI), color = "blue") + # upper
  geom_line(aes(y = predicted - CI), color = "red") + # lower
  theme_classic()

參考:
https://stackoverflow.com/questions/23519224/extract-r-square-value-with-r-in-linear-models (提取R2)
https://blog.csdn.net/LeaningR/article/details/118971000 (提取R2等)
https://stackoverflow.com/questions/45742987/how-is-level-used-to-generate-the-confidence-interval-in-geom-smooth (添加lm線)
https://zhuanlan.zhihu.com/p/131604431 (知乎)

到此這篇關(guān)于R語言在散點(diǎn)圖中添加lm線性回歸公式的問題的文章就介紹到這了,更多相關(guān)R語言線性回歸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • R語言中向量和矩陣簡單運(yùn)算的實(shí)現(xiàn)

    R語言中向量和矩陣簡單運(yùn)算的實(shí)現(xiàn)

    這篇文章主要介紹了R語言中向量和矩陣簡單運(yùn)算的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • R語言 實(shí)現(xiàn)選取某一行的最大值

    R語言 實(shí)現(xiàn)選取某一行的最大值

    這篇文章主要介紹了R語言 實(shí)現(xiàn)選取某一行的最大值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言 檢驗(yàn)多重共線性的操作

    R語言 檢驗(yàn)多重共線性的操作

    這篇文章主要介紹了R語言 檢驗(yàn)多重共線性的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言ggplot2設(shè)置圖例(legend)的操作大全

    R語言ggplot2設(shè)置圖例(legend)的操作大全

    ggplot2是一個(gè)繪制可視化圖形的R包,汲取了R語言基礎(chǔ)繪圖系統(tǒng)(graphics)和l?attice包的優(yōu)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于R語言ggplot2設(shè)置圖例(legend)的操作大全,需要的朋友可以參考下
    2022-07-07
  • R語言學(xué)習(xí)初識(shí)Rcpp類型List

    R語言學(xué)習(xí)初識(shí)Rcpp類型List

    這篇文章主要為大家介紹了R語言中Rcpp的類型List的基礎(chǔ)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-11-11
  • R語言ggplot2圖例標(biāo)簽、標(biāo)題、順序修改和刪除操作實(shí)例

    R語言ggplot2圖例標(biāo)簽、標(biāo)題、順序修改和刪除操作實(shí)例

    R語言中畫圖時(shí)我們常常要設(shè)定標(biāo)題,如果圖片標(biāo)題是固定則很容易操作,下面這篇文章主要給大家介紹了關(guān)于R語言ggplot2圖例標(biāo)簽、標(biāo)題、順序修改和刪除操作的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • R語言版本升級(jí)完全攻略基于Ubuntu

    R語言版本升級(jí)完全攻略基于Ubuntu

    這篇文章主要為大家介紹了在Ubuntu上針對(duì)不同的版本對(duì)R語言版本升級(jí)的完全攻略,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • 使用ggsignif優(yōu)雅添加顯著性標(biāo)記詳解

    使用ggsignif優(yōu)雅添加顯著性標(biāo)記詳解

    這篇文章主要為大家介紹了使用ggsignif優(yōu)雅添加顯著性標(biāo)記詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • R語言 如何保留大于或小于特定數(shù)值的行

    R語言 如何保留大于或小于特定數(shù)值的行

    這篇文章主要介紹了R語言實(shí)現(xiàn)保留大于或小于特定數(shù)值的行操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言dplyr包之高效數(shù)據(jù)處理函數(shù)(filter、group_by、mutate、summarise)詳解

    R語言dplyr包之高效數(shù)據(jù)處理函數(shù)(filter、group_by、mutate、summarise)詳解

    這篇文章主要介紹了R語言dplyr包之高效數(shù)據(jù)處理函數(shù)(filter、group_by、mutate、summarise)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03

最新評(píng)論