简体   繁体   English

从已知回归方程绘制回归线

[英]Plotting a regression line from a known regression equation

I have the following code that calculates a non-parametric regression equation (not lm) from all pairs of data.我有以下代码从所有数据对计算非参数回归方程(不是 lm)。 In this code 'mdns' is the median slope value and 'mdni' is the median intercept so I would then like to plot the points (all x,y pairs) from the data in a scatter plot and then plot the regression line (1) using lm from the raw data and (2) using the calculated non-parametric regression values in the standard equation y=mdni+mdns(X).在这段代码中,“mdns”是斜率中值,“mdni”是中值截距,所以我想从散点图中的数据中绘制点(所有 x,y 对),然后绘制回归线(1 ) 使用来自原始数据的 lm 和 (2) 使用标准方程 y=mdni+mdns(X) 中计算的非参数回归值。 I know how to do everything except this make a line in a plot from a known equation.我知道如何做所有事情,除了这个在已知方程的图中画一条线。

#Calculate pairs of all Slope values
slope<-vector()
 for (i in 1:n){
 for(j in 2:n){ 
   temps<-(data1$y_book[j]-data1$y_book[i])/(data1$x_book[j]-data1$x_book[i])
 if (j>i){  
 slope<-append(slope,temps)}
 }
 }

slope<-slope[!is.na(slope)]
print(slope)
mdns<-median(slope)

#Calculate the Intercept values
inter<-vector()
for (k in 1:n){
    tempi<-(data1$y_book[k] - (mdns*data1$x_book[k]))
    
 inter<-append(inter,tempi)
}
#echo results
print(inter)
mdni<-median(inter)

Not sure what you're looking for, but there are 2 basic ways of plotting a line.不知道你在找什么,但有两种绘制线条的基本方法。

using abline()使用 abline()

plot(NULL,ylim=c(-10,10),xlim=c(-10,10)
abline(a=.4,b= 1) # a= slope, b= intercept (see docs for other options)
# Note line extends completely across plot.

using lines()使用行()

plot(NULL,ylim=c(-10,10),xlim=c(-10,10))
x <- seq(from=-9,to=9,length.out=30) 
y <- seq(from=-3,to=3,length.out=30)
lines(cbind(x,y))
# Note line covers only defined extent

See plot, lines, abline documentation.请参阅绘图、线条、abline 文档。 Also see ggplot.另见ggplot。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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