0

I try to calculate the elements of a matrix, I wrote the elements normarly but when I run it, I received this error message:

I[1,1] <- ((1/n)*sum(delta*(((1/alp)-h(x)-(M(x)/(1-W(x))))^2)))

Error in I[1, 1] <- ((1/n) * sum(delta * (((1/alp) - h(x) - (M(x)/(1 -  :  object of type 'closure' is not subsettable

I[2,2] <- ((1/n)*sum(delta*(((1/b)+log(A(x))-R(x)))^2))

Error in I[2, 2] <- ((1/n) * sum(delta * (((1/b) + log(A(x)) - R(x)))^2)) :  object of type 'closure' is not subsettable

I[3,3] <- ((1/n)*sum(delta*(((1/d)-(x^gam)-T(x)+((x^gam)))/((((1-exp(-d*x^gam)))*B(x))^2))))

Error in I[3, 3] <- ((1/n) * sum(delta * (((1/d) - (x^gam) - T(x) + ((x^gam)))/((((1 -  : object of type 'closure' is not subsettable

I[4,4] <- ((1/n)*sum(delta*((1/gam)+log(x)-(d*(x^gam)*log(x))*D(x)-((d*(x^gam)*log(x)*exp(-d*x^gam))/(1-exp(-d*x^gam)))*N(x))^2))

Error in I[4, 4] <- ((1/n) * sum(delta * ((1/gam) + log(x) - (d * (x^gam) *  : object of type 'closure' is not subsettable


I am not sure why as I changed nothing in my code.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • If `A`, `R` , etc. are vectors or matrices, they should be accessed via `[ ]`, not `( )` Also, check out https://stackoverflow.com/questions/11308367/error-in-my-code-object-of-type-closure-is-not-subsettable – boski Feb 12 '19 at 10:35
  • 1
    Please [format your code properly](https://meta.stackexchange.com/a/22189/371738), thanks! – jay.sf Feb 12 '19 at 10:37
  • The problem is that you have not defined `I` before assigning into it and `I` is a function (closure) in R. – Roland Feb 12 '19 at 11:08

1 Answers1

2

I() is a function which inhibits the interpretation of an expression. It could be bound to a matrix value, nevertheless, but my guess (given the lack of context) is that you did not assign your matrix to I but to something else:

> n = 10
> b = 20
> A = function(x) {x+1}
> R = function(x) {x*2}
> delta = 20
> x = 4
> I[1,1] <- ((1/n)*sum(delta*(((1/b)+log(A(x))-R(x)))^2))
Error in I[1, 1] <- ((1/n) * sum(delta * (((1/b) + log(A(x)) - R(x)))^2)) :
  object of type 'closure' is not subsettable
> I
function (x)
{
    structure(x, class = unique(c("AsIs", oldClass(x))))
}
<bytecode: 0x2fdae80>
<environment: namespace:base>
> I = matrix(c(1,2,3,4), nrow=2)
> I[1,1] <- ((1/n)*sum(delta*(((1/b)+log(A(x))-R(x)))^2))
> I
         [,1] [,2]
[1,] 80.40546    3
[2,]  2.00000    4
Bob Zimmermann
  • 938
  • 7
  • 11