显示方程式并显示二次回归 [英] Plot quadratic regression with equation displayed

查看:81
本文介绍了显示方程式并显示二次回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用for循环创建一个包含6个图(3行2列)的pdf页面.我能够创建图,但是我似乎无法自动向每个图添加回归线.

I am trying to create a pdf page conatining 6 plots (3 rows and 2 columns) using a for loop. I am able to create the plots but i cant seem to automate adding a regression line to each plot.

我正在尝试以下代码.

I am trying the following code.

#Dummy data
Data1 <- data.frame(flow = c(8,8.5,6,7.1,9), SP_elev = c(20,11,5,25,50))
Data2 <- data.frame(flow = c(7,7.2,6.5,8.2,8.5), SP_elev = c(13,15,18,25,19))
Data3 <- data.frame(flow = c(2,3,5,7,9), SP_elev = c(20,25,28,30,35))
Data4 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data5 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data6 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(22,23,25,27,29))

#Create Vector list 
dataframes = list("Data1" = Data1, 
                  "Data2" = Data2, 
                  "Data3" = Data3,
                  "Data4" = Data4,
                  "Data5" = Data5,
                  "Data6" = Data6) # I gave up here

# open the PDF device
pdf(file="Dummy_Example.pdf", paper="letter", height=10, width=8)

#Create array of plots 
par(mfrow=c(3,2))

#plot a with regression model
for (i in dataframes) {

plot (i[,c('flow', 'SP_elev')], xlab=expression(paste("Discharge (", ft^3, "/s)",sep = "")), ylab= "Elevation (m)", tck=0.02, adj = 0.5)

#plot regression curve
fit2<-lm(i$SP_elev ~ i$flow + I(i$flow^2), data=i) 
pol2 <- function(x) fit2$coefficient[3]*x^2 + fit2$coefficient[2]*x + fit2$coefficient[1] 
curve(pol2, lwd=1, add=T)
}

# close the PDF device
dev.off()

当我单独制作一个绘图时,我得到了回归曲线代码,但是当我尝试使其自动化时,它似乎不起作用.

I got the regression curve code to work when i am individually producing a plot but it wont seem to work when i try to automate it.

此外,我还想在图表上绘制回归曲线的方程.

In addition I also want to plot the equation of the regression curve on the graph.

谢谢

dubbdan

推荐答案

我编辑了上面的代码以将等式包含在图中.更新:现在更漂亮的方程式.

I edited the code above to include the equation in the plot. Update: Now prettier equation.

#Dummy data
Data1 <- data.frame(flow = c(8,8.5,6,7.1,9), SP_elev = c(20,11,5,25,50))
Data2 <- data.frame(flow = c(7,7.2,6.5,8.2,8.5), SP_elev = c(13,15,18,25,19))
Data3 <- data.frame(flow = c(2,3,5,7,9), SP_elev = c(20,25,28,30,35))
Data4 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data5 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data6 <- data.frame(flow = c(1,4,6,8,9), SP_elev = c(22,23,25,27,29))

#Create Vector list 
dataframes = list("Data1" = Data1, 
                  "Data2" = Data2, 
                  "Data3" = Data3,
                  "Data4" = Data4,
                  "Data5" = Data5,
                  "Data6" = Data6) # I gave up here

# open the PDF device
pdf(file="Dummy_Example.pdf", paper="letter", height=10, width=8)

#Create array of plots 
par(mfrow=c(3,2))

#plot a with regression model
for (i in dataframes) {

plot (SP_elev ~ flow, data=i,
      xlab=expression(paste("Discharge (", ft^3, "/s)",sep = "")),
      ylab= "Elevation (m)", tck=0.02, adj = 0.5)

#plot regression curve
fit2<-lm(SP_elev ~ flow + I(flow^2), data=i) 
pol2 <- function(x) fit2$coefficient[3]*x^2 + fit2$coefficient[2]*x + fit2$coefficient[1] 
curve(pol2, lwd=1, add=T, col="blue")

xm <- min(i$flow)
ym <- max(i$SP_elev)

co <- signif(coef(fit2),3)
text(xm, ym,
 bquote(y==.(co[3])*x^2 + .(co[2])*x + .(co[1])),
 adj=c(0,1))


}
dev.off()

这篇关于显示方程式并显示二次回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆