-1

I want the app that I am developing automatically go to the excel sheet and calculate the transition probability matrix.

 action<-actions
    state<-states
    p<-array(0,c(length(state),length(state),length(action)))
    colnames(p)=state
    rownames(p)=state
    # transition probability matrix based on the action that we have choosen
    empiricala1<-read.xlsx("www/dynamic model.xlsx",1)
    empiricala2<-read.xlsx("www/dynamic model.xlsx",2)


    #show(empirical) calculate transition probability from historical data
    em1<-as.data.frame(empiricala1)
    em2<-as.data.frame(empiricala2)
    tab1 <- table(em1)
    tab2 <- table(em2)

    tab1<-addmargins(prop.table(table(em1$current,em1$nextstate),1),2)
    tab2<-addmargins(prop.table(table(em2$current,em2$nextstate),1),2)
    transitionprob1<-p[,,1]<-prop.table(table(em1$current,em1$nextstate),1)
    transitionprob2<-p[,,2]<-prop.table(table(em2$current,em2$nextstate),2)
    print(transitionprob1)
    print(transitionprob2)


    for(i in 1:length(action)){

      p[,,i]<-prop.table(table(em[i]$current,em[i]$nextstate),i)

    }

The error that I got is as following:

Error in em[i] : object of type 'closure' is not subsettable

How can I fix this problem.

user
  • 592
  • 6
  • 26
  • Where is `em` declared? As to your error, see `?em` – MichaelChirico Feb 27 '16 at 00:22
  • The interpreter thinks that `em` is a function. – IRTFM Feb 27 '16 at 00:23
  • I want to address em1 and em2 whre you can see in the 'em1<-as.data.frame(empiricala1) em2<-as.data.frame(empiricala2)' – user Feb 27 '16 at 00:24
  • can you please tell me how to fix this problem – user Feb 27 '16 at 00:28
  • 1
    `lis <- list(em1,em2); for (i in 1:length(lis)) p[,,i]<-prop.table(table(lis[[i]][,"current"],lis[[i]][,"nextstate"]),i)` – slamballais Feb 27 '16 at 00:50
  • Thanks it works, can you please explain why did you make a list from em1 and em2. – user Feb 27 '16 at 00:55
  • 2
    Because you were using a `for`-loop, so you want to iterate over something. You tried `em[i]`, but that just means that you have a vector `em` and want to iterate over its elements (`em[1]`, `em[2]`), NOT that you want `em1`, `em2`, etc. That's why the error occurred. To solve this, I just put `em1` and `em2` in a list, so that you can iterate over the list instead. – slamballais Feb 27 '16 at 01:21
  • Thanks for your constructive explanation – user Feb 27 '16 at 01:24

1 Answers1

1

Ok, so to expand on the comments...

You have two data frames em1 and em2. You seem to want to apply the same operations to both data frames (e.g. table(em1) and table(em2). This becomes really tedious to write, especially once you get way more variables.

What you tried to do, was:

for (i in 1:2) em[i]

The problem with this is that you do NOT get em1 and em2. Instead, you get em[1] and em[2] and are thus referring to the em object (which does not exist to begin with).

There are a couple of ways to solve this.

1. Move the data frames to a list

lis <- list(em1, em2)

This way, you can iterate over the list with, for example, the *apply family or a for-loop:

sapply(lis, nrow)
for (i in 1:length(lis)) nrow(lis[[i]])

2. Use get

Another option is to use get, which allows you to provide a string and it will subsequently return the variable described in that string.

vec <- c("em1","em2")
sapply(vec, function(x) nrow(get(x)))

Note that using get is generally discouraged. I'd also opt for the first option instead.

slamballais
  • 3,161
  • 3
  • 18
  • 29