[R] Writing functions. please help

guilherme dantas guibds at gmail.com
Thu Jan 20 15:47:12 CET 2011


Hi, my name is Guilherme and I'm trying to solve an optimization problem in
R, regarding reliability and survival time of equipments. I Have to write
the function in the image attached to this email, please take a look, where
the f(t) is weibull's distribution density function, Ca=1000 and Cb=100 are
costs of two equipments and i'm using shape=2.5 and scale=20. However, a few
problems have appeared:

1- at the beginnig I thought of using the function dweibull, but the input
is a vector of quantiles and my objective is to work with different time
periods. So I chosed to write weibull's function already with the parameters
shape and scale pre-defined, even though I know that's not the best option.
So I have the following code which works perfectly:

 >weibull=function(t){  +dist=(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
+return(dist)}
and then i used that to write as follows, use shape=2.5, scale=20, and
transfering Ca and Cb to the integral's interior:

func=function(t){
func1=function(t){
weibull=function(t){
dist=1000*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
return(dist)}
calc=integrate(weibull, 0, t)
return(calc)}
func2=function(t){
weibull=function(t){
dist=100*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
return(dist)}
calc=integrate(weibull,t, 100)
return(calc)}
func3=function(t){
weibull=function(t){
dist=t*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
return(dist)}
calc=integrate(weibull,0 ,t)
return(calc)}
func4=function(t){
weibull=function(t){
dist=t*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
return(dist)}
calc=integrate(weibull,t ,100)
return(calc)}
lastcalc=(func1+func2)/(func3+func4)
return(lastcalc)}

appears the following: Error in func1 + func2 : non numeric operator to
binary operator

2-How the function is "composed" by four integrations, two on the numerator
and the other tow on the denominator, and how i was not being succesful in
writing the function directly, I wrote these four integrals
separately,considering already the shape, the scale, Ca, Cb as given. The
code is the following:

2.1: for the first part in the numerator
> intWeibull1=function(t){
+ integr=function(t) 1000*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
+ calc=integrate(integr, 0, t)
+ return(calc)}

2.2: for the second part in the numerator
> intWeibull2=function(t){
+ integr=function(t) 100*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
+ calc=integrate(integr,t,100)
+ return(calc)}

2.3: for the third part, the first in the denominator
> intTweibull=function(t){
+ integr=function(t) t*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
+ calc=integrate(integr,0,t)
+ return(calc)}

2.4: for the last part, the secondin the denominator
> intTweibull2=function(t){
+ integr=function(t) t*(2.5/20)*(t/20)^(2.5-1)*exp(-(t/20)^2.5)
+ calc=integrate(integr,t,100)
+ return(calc)}

3-All the functions above are working perfectly, but when i try to put
everything together trough this code and call the function, appears a error
message:

> FUNC=function(f1,f2,f3,f4,t){
+ a=f1(t); b=f2(t); c=f3(t); d=f4(t)
+ calc=(a+b)/(c+d)
+ return(calc)}
>FUNC(intWeibullmil,intWeibullcem,intTweibull,intTweibull2,3) # t=3 is just
an example

Error in a+b: non numeric argument to binary operator

which I don't understand, becouse in previous examples I used the same
programming intuition and it worked,for example:

> f1=function(x){
+ calc=2*x
+ return(calc)}
> f2=function(x){
+ calc=3*x
+ return(calc)}
> f4=function(fun1,fun2,x){
+ a=fun1(x);b=fun2(x)
+ calc=a+b
+ return(calc)}
> f4(f1,f2,2)
[1] 10

4-another doubt is: even if I manage to write this function (FUNC), when I
try to optimize it, will it be equivalent to optimize a one variable
function? (which is not very complex) or it means I'm gonna have to optimize
a five variable function? (which is really hard). Are there any packages
able to help me out here, I mean with writing weibull's distribution and
optimization issues?

Any suggestions of how i can write that differently, or what am I missing
here will be more than welcome.

Thank you so much for your attention, Guilherme


More information about the R-help mailing list