简体   繁体   中英

How to plot regression line only through data range

I am having problems plotting a regression line that only covers the range of the x values. The only solution I get to work is similar to using abline , which produces regression lines through the entire plot.

My data frame 'rates':

structure(list(subpop = structure(1:13, .Label = c("BB", "DS", 
"FB", "GB", "KB", "LS", "MC", "NB", "NW", "SB", "SH", "VM", "WH"), 
class = "factor"), totharv = c(5902, 3187, 5429, 1513, 880, 
4027, 1100, 1642, 197, 2811, 2477, 173, 1747), annumean = c(12.321503131524, 
65.0408163265306, 110.795918367347, 30.8775510204082, 17.9591836734694, 
82.1836734693878, 22.4489795918367, 33.5102040816327, 4.02040816326531, 
57.3673469387755, 50.5510204081633, 3.53061224489796, 35.6530612244898),
totarea = c(1078840.89485, 2620106.29649, 1181019.17255, 171136.23318, 
156514.755664, 487532.074005, 495256.254596, 944667.823467, 147262.719799, 
715030.748752, 1135249.6887, 209962.017835, 502379.942988), prcprime = c(31, 
42, 96, 98, 67, 70, 99, 23, 74, 60, 100, 62, 100), prime = c(334440.6774035, 
1100444.6445258, 1133778.405648, 167713.5085164, 104864.88629488, 
341272.4518035, 490303.69205004, 217273.59939741, 108974.41265126, 
429018.4492512, 1135249.6887, 130176.4510577, 502379.942988), 
prey = c(8, 8, 7, 5, 5, 6, 2, 3, 2, 4, 5, 2, 5), density = c(2.62, 
1.27, 2.19, 9.3, 2.28, 5.21, 0.57, 1.04, 1.38, 1.27, 0.69, 
0.77, 2.05), prime1000 = c(334.4406774035, 1100.4446445258, 
1133.778405648, 167.7135085164, 104.86488629488, 341.2724518035, 
490.30369205004, 217.27359939741, 108.97441265126, 429.0184492512, 
1135.2496887, 130.1764510577, 502.379942988), totarea1000 = c(1078.84089485, 
2620.10629649, 1181.01917255, 171.13623318, 156.514755664, 
487.532074005, 495.256254596, 944.667823467, 147.262719799, 
715.030748752, 1135.2496887, 209.962017835, 502.379942988
), meanrate = c(8.70146823014459, 4.93980932053221, 4.31244487144207, 
2.27890716207299, 6.83701834392294, 3.19292620629519, 3.07469056764831, 
2.9169758812616, 1.43304970891178, 3.60271608599471, 5.95451287881945, 
1.09586402811883, 3.65560375696448)), row.names = c(NA, -13L), 
class = "data.frame")

The script that plots the data points:

plot(rates$prime1000,rates$meanrate,pch=19,cex=1.7,ylim=c(0,10),
    xlim=c(0,1250),lwd=2,las=1,cex.lab=1.3,
ylab="Mean rate (%)",xlab="Shelf area (1000 sq.km)")
text(rates$prime1000,rates$meanrate,labels=rates$subpop,
    cex=0.9,pos=2,offset=0.7)

And the script supposed to plot the regression line across the range of the x values only, which sadly does not work:

fit <- lm(rates$meanrate~rates$prime1000)
x0 <- seq(min(rates$prime1000), max(rates$prime1000))
y0 <- predict.lm(fit, newdata = list(prime1000 = x0),length=20)
lines(x0, y0, col = 2)

Might be other ways to do it, but does anyone see the problem in this script?

The predict function must include the exact same variable as in the fit model. Instead of including rates$prime1000 (and rates$meanrate ), would separate out dataframe source:

fit <- lm(meanrate~prime1000, data = rates)

带有回归线的绘图

在此处输入图像描述

Your code works well? I have just changed the length of prediction to match that of x0 .

fit <- lm(meanrate ~ prime1000, data=rates)
x0 <- seq(min(rates$prime1000), max(rates$prime1000))
y0 <- predict.lm(fit, newdata = list(prime1000 = x0))
lines(x0, y0, col = 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM