将用黄土绘制的线分成R中的两个部分

时间:2014-05-15 09:02:57

标签: r graph loess

我试图用命令loess在R中做出最佳拟合,但问题是它应该存在两个部分,而我的数据只有一个向量......

所以我先得到这个命令:

x = 1:generaties
CI.up = as.numeric(gemiddelde)+as.numeric(ci)
CI.dn = as.numeric(gemiddelde)-as.numeric(ci)
plot(gemiddelde~x, cex=0.5,xaxt='n',ylim=c(0,1.1), xlab='Populatie(a = 100, size = 500, n = 15, tau = 10, c = 50, sigma = 0.5, phenoNumber = 5)',ylab='Fitness', main='Fitness over  generaties',col='blue',pch=16)
axis(1, at=x)
arrows(x,CI.dn,x,CI.up,code=3,length=0.05,angle=90,col='red')

这给了我一个图表

然后我尝试添加:

x1 = 1:(generaties/2)
x2 = (generaties/2):generaties
lo1 <- loess(gemiddelde[1:(generaties/2)]~x1)
lo2 <- loess(gemiddelde[(generaties/2):generaties]~x2)
lines(predict(lo1), col='red', lwd=2)
lines(predict(lo2), col='red', lwd=2)

然后我从我的图形的一半开始从0开始,而不是从第二个开始。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

我认为你几乎就在那里 - 只需要为lines()命令指定一些x值。希望我创建的数据具有足够的代表性:

#Make up some data
set.seed(0)
generaties <- 20
gemiddelde <- 0.5+0.2*sin(1:generaties)
ci <- runif(generaties)*0.3

#your plot
x = 1:generaties
CI.up = as.numeric(gemiddelde)+as.numeric(ci)
CI.dn = as.numeric(gemiddelde)-as.numeric(ci)
plot(gemiddelde~x, cex=0.5,xaxt='n',ylim=c(0,1.1), xlab='Populatie(a = 100, size = 500, n = 15, tau = 10, c = 50, sigma = 0.5, phenoNumber = 5)',ylab='Fitness', main='Fitness over  generaties',col='blue',pch=16)
axis(1, at=x)
arrows(x,CI.dn,x,CI.up,code=3,length=0.05,angle=90,col='red')

#Adding the loess smoother
x1 = 1:(generaties/2)
x2 = (generaties/2):generaties
lo1 <- loess(gemiddelde[1:(generaties/2)]~x1)
lo2 <- loess(gemiddelde[(generaties/2):generaties]~x2)
lines(x1,predict(lo1), col='red', lwd=2)  #specify x values
lines(x2,predict(lo2), col='red', lwd=2)  #specify x values