insongkim / PanelMatch

111 stars 34 forks source link

Fix panelmatch moderator error #108

Closed hjdruc closed 1 year ago

hjdruc commented 2 years ago

Thanks for the amazing panelmatch package.

When I set moderator <- var.name, sometimes I got the error message: Error in names(ret.obj) <- as.character(as.vector(na.omit(moderating.values))): 'names' attribute [4] must be the same length as the vector [3]

Finally I found the reason is: In fuction "handle_moderating_variable" which is in "PanelMatch\R\PE_lower_level.R", although moderator has 4 value (by: moderating.values <- unique(ordered.data[, moderator]), the actual number of ret.obj is only 3 (induced by: moderated.sets.att[[make.names(val)]] <- NULL). Thus, when ret.obj (the length is 3) was set attribute name (the lenth is 4), the errror occured.

Currently, I fix this error by (still in fuction "handle_moderating_variable"): ... valid.moderating.values.vector <- c() # a vector to replace moderating.values for(val in as.vector(na.omit(moderating.values))) {

make sure we handle empty set situation

if(!is.null(att.sets))
{
  indx.set <- moderator.vector[names(att.sets)] == val
  t.set <- att.sets[indx.set]
  if(length(t.set) > 0)
  {
    moderated.sets.att[[make.names(val)]] <- t.set

    # only valid val is added to vector
    valid.moderating.values.vector <-c(valid.moderating.values.vector, val) 
  }
  else
  {
    moderated.sets.att[[make.names(val)]] <- NULL
  }
}
if(!is.null(atc.sets))
{
  indx.set <- moderator.vector[names(atc.sets)] == val
  t.set <- atc.sets[indx.set]
  if(length(t.set) > 0)
  {
    moderated.sets.atc[[make.names(val)]] <- t.set

    # only valid val is added to vector
    valid.moderating.values.vector <-c(valid.moderating.values.vector, val) 
  }
  else
  {
    moderated.sets.atc[[make.names(val)]] <- NULL
  }

...

use valid value as vector

names(ret.obj) <- as.character(as.vector(valid.moderating.values.vector))
return(ret.obj)

By this way, I can deal with the problem in my own case. If this is the right way, how about fix it in a better and formal way when launch the next Panelmatch version?

Thanks for your excellent and hard work.

insongkim commented 2 years ago

@hjdruc Thank you very much. We will look into this and will incorporate necessary changes in our next update. Thank you very much. We really appreciate it.

adamrauh commented 1 year ago

This should be fixed in the latest version of the se_comparison branch 👍