如何基于先前迭代的结果循环一个函数

时间:2019-04-22 18:22:43

标签: r function loops iteration nls

我有一个线性平稳函数。我希望该函数排除小于所达到最大值的10%的y值(意味着平稳期的y值)并进行迭代,直到不发生任何变化(即找不到比其最大值大10%的值)。

这里是一个例子:

#create data 
x=c(1:7,1:7)
y=c(2,10,21,27,35,33,35,2,9,20,28,32,30,31)
df<-data.frame(x,y)
plot(x,y)

#create linear-plateau function
#a and b are intercept and slope for x<c, and c is the breakpoint
lp <-function(x, a, b, c){
  ifelse(x > c, a + b * c, a + b * x)
  }

#fit the model
fit1=nls(y ~ lp(x, a, b, c), data = df, start = list(a = 0, b = 10, c = 5))

> coef(fit1)
        a         b         c 
-7.000000  8.750000  4.533333 

最大y值(表示平稳期的y值)达到x=c,可以计算为ymax=a+b*c

问题:
如何使该函数排除小于最大值的10%的值并进行迭代,直到未观察到任何变化?

0 个答案:

没有答案
相关问题