0

I receive this error:

   > dMMSE(c(2,1))
 Show Traceback

 Rerun with Debug
 Error in abs(t) <= k : 'k' is missing 

Here's my code:

rho.prime<-function(t,k) ifelse (abs(t)<=k,2*t,(2*k*sign(t)))
dMMSE <- function(b, y=farmland$farm, x=farmland$land){

  n = length(y)
  a=0
  d=0
  for (i in 1:n) {

    a = a + rho.prime(y[i]-b[1]-b[2]*x[i])
    d = d + x[i]*rho.prime(y[i]-b[1]-b[2]*x[i])
  }
  a <- (-a/n)
  d <- (-d/n)
  return(c(a,d))
}

Any idea what I am missing or doing wrong?

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • 5
    You are attempting to subset `rho.prime` using square brackets, but it's a function. Maybe you meant those to be function calls? – joran Apr 22 '14 at 22:32
  • 1
    It is not b[0] and b[1] you want but b[1] and b[2] to get the first and second elements of b – user20650 Apr 22 '14 at 22:32
  • joran you were correct. I fixed that but now I have this error: `Error in if (abs(t[i]) <= k) { : missing value where TRUE/FALSE needed ` – Mona Jalal Apr 22 '14 at 22:48
  • 3
    Did you even search at all before posting this question? The title of your question is practically IDENTICAL to a [previous question](http://stackoverflow.com/questions/11308367/object-of-type-closure-is-not-subsettable), which would have popped up in the "*Questions that may already have your answer*" when you wrote it in. Your new error is also the same error you had in the [question you posted yesterday](http://stackoverflow.com/questions/23209430/missing-value-where-true-false-needed-in-r). C'mon... – thelatemail Apr 22 '14 at 23:22
  • 4
    I was disappointed that while you accepted [my answer to your other question on nearly the same topic](http://stackoverflow.com/questions/23208556/writing-a-function-that-operates-on-a-vector-in-r/23208589?noredirect=1#comment35503151_23208589) (but not mentioned by @thelatemail), you did not implement it in your function. – nograpes Apr 22 '14 at 23:28
  • @nograpes I updated the question and used your method yet I receive k is missing error. `Error in abs(t) <= k : 'k' is missing ` – Mona Jalal Apr 22 '14 at 23:59
  • 2
    You need to pass k as an argumeny to `dMMSE` or set a default in `rho.prime` – user20650 Apr 23 '14 at 00:01
  • I thought it is hardwired to rho.prime() and it's not needed to do so as I earlier had set it to k=19000 – Mona Jalal Apr 23 '14 at 00:02
  • but you haven't set it to 19000 – user20650 Apr 23 '14 at 00:03
  • @user20650 Thank you it worked. Someone told if I hardwire a constant in my program I don't really need to pass it to all of my functions. Thanks. Would you please write it as an answer? – Mona Jalal Apr 23 '14 at 00:05
  • 1
    @MonaJalal; your welcome..please write an answer. But if you are going to vary `k` it is probably best to add it aas an argument to `dMMSE` [ie `dMMSE <- function(b, y=farmland$farm, x=farmland$land, k=19000){`] and add the `k` to the rho.prime calls in the `dMMSE` function [ie `a = a + rho.prime(y[i]-b[1]-b[2]*x[i],k)`] and the same for `d` – user20650 Apr 23 '14 at 00:13

0 Answers0