简体   繁体   English

R中线性回归的图非线性图

[英]Plot Non-linear Plot for Linear Regression in R

y<-c(0.0100,2.3984,11.0256,4.0272,0.2408,0.0200);
x<-c(1,3,5,7,9,11);
d<-data.frame(x,y)
myLm<-lm(x~y**2,data=d)
plot(d)
lines(x,lm(y ~ I(log(x)) + x,data=d)$fitted.values)
lines(x,lm(y ~ I(x**2) + x,data=d)$fitted.values) % not quite right, smooth plz

It should be smooth plot, something wrong. 应该是平滑的情节,出了点问题。

在此处输入图片说明

Helper questions 帮手问题

  1. What algorithm is used in linear regression? 线性回归使用什么算法?
  2. Explain least squares plotting with Ones -matrix 解释用Ones -matrix绘制的最小二乘法

You need predict in order to interpolate the predictions between the fitted points. 您需要predict以便在拟合点之间内插预测。

d <- data.frame(x=seq(1,11,by=2),
                y=c(0.0100,2.3984,11.0256,4.0272,0.2408,0.0200))
lm1 <-lm(y ~ log(x)+x, data=d)
lm2 <-lm(y ~ I(x^2)+x, data=d)
xvec <- seq(0,12,length=101)
plot(d)
lines(xvec,predict(lm1,data.frame(x=xvec)))
lines(xvec,predict(lm2,data.frame(x=xvec)))

在此处输入图片说明

The mandatory ggplot2 method: 强制ggplot2方法:

library(ggplot2)
qplot(x,y)+stat_smooth(method="lm", formula="y~poly(x,2)", se=FALSE)

在此处输入图片说明

something like: 就像是:

 plot(d)    
 abline(lm(x~y**2,data=d), col="black")

will make it (if linear, as was implied by the way the question was asked first) 将使其(如果是线性的,则如先问问题的方式所暗示)

For what you are looking for I think: 对于您正在寻找的东西,我认为:

  lines(smooth.spline(x, y))

May work as hinted by Dirk. 可能按Dirk的提示工作。

You should spend some time with the 'Appendix A: A sample session' of the 'An Introduction R' manual that came with your program. 您应该花一些时间在程序随附的“ Introduction R”手册的“附录A:示例会话”中。 But here is a start 但这是一个开始

R> y<-c(0.0100,2.3984,11.0256,4.0272,0.2408,0.0200);
R> x<-c(1,3,5,7,9,11);
R> d<-data.frame(x,y)
R> myLm<-lm(x~y**2,data=d)
R> myLm

Call:
lm(formula = x ~ y^2, data = d)

Coefficients:
(Intercept)            y  
      6.434       -0.147  

and we can plot this as (where I now corrected for your unusual inversion of the roles of x and y ): 我们可以将其绘制为(现在我在这里纠正了xy角色的异常反转):

R> plot(d)
R> lines(d$y,fitted(myLm))

在此处输入图片说明

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

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