r - Optimizing a simple linear curve (constant and coefficient estimated from a regression) -
i trying calculate turning point of a few functions have estimated coefficient , constant regression. i'm using optimize function curves linear.
my function looks like: f<- function(x){ beta* x + alpha }
mind: beta , alpha both vectors here. when running optimisation optimize, i'm getting following error:
error in optimize(f, interval = c(10, 20), lower = (10), : invalid function value in 'optimize'
is because optimize running optimisation mathematically, beta , alphas need single parameters? if knows better way of doing please contribute!
thank in advance :)
if functions linear, @ minimum @ lower end of range beta>=0, , @ upper end of range if beta<=0 - no need use optimize()
.
it's not entirely clear you're expecting code - if want return x
each set of parameters, @ optim()
instead , have f return sum, or run optimize on each set of parameters in turn using apply()
function or loop.
one other thing syntax bit wonky - imagine mean:
> f<- function(x){ + beta* x + alpha + } > alpha <- 1 > beta <- 2 > optimize(f,c(10,20)) $minimum [1] 10.00006 $objective [1] 21.00011
Comments
Post a Comment