-2

I want to minimize a function (rmse) in R just like solver does in excel. Using the constrained vaiables (i) and conditioning it for

**i >= 0 && i<=2**

ac = c(85,95,79,88,90,99,111,99,100,110)
ff = c(100,110,105,95,115,105,110,120,105,110)
ff1 = ff[2:5] ;ac1 = ac[2:5]
i=1.1 #Assume-Constraint variable
revff = ff1*i
dev1 = abs(ac1-revff)
rmse_function = function(ac1,ff1,i) sqrt(sum(abs(ac1-ff1*i)^2))

I want to minimize the function rmse by changing the variable i.

Teja S
  • 19
  • 1
  • 5

1 Answers1

0

Write your function so its the first argument you want to minimise over, use optimise:

> rmse_function = function(i,ac1,ff1)sqrt(sum(abs(ac1-ff1*i)^2))
> optimise(rmse_function,c(0,2),ac1=ac1, ff1=ff1)
$minimum
[1] 0.8254548

$objective
[1] 13.87804

Hence the minimum is at i=0.825 and the function has a value of 13.87 there.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Thank you! That worked like a charm, now I've even wrote it for Weighted Average and MAE too. – Teja S Jan 27 '17 at 14:16