2

I need to change few lines in a function in package called mirt.

That function is fscores.internal and is not a main function where you can just use edit or fix to change the code lines.

How can do this in RStudio?

I tried following:

library(mirt)
data(LSAT7)
LSAT7=expand.table(LSAT7)
mod=mirt(LSAT7, 1,itemtype = '3PL')
fscor=fscores(object = mod,method = "ML",response.pattern = c(1,NA,NA,NA,NA),theta_lim = c(-4, 4))


fscor
     Item.1 Item.2 Item.3 Item.4 Item.5  F1 SE_F1
[1,]      1     NA     NA     NA     NA Inf    NA

This is the final output. I need "theta_lim[2]" instead of "Inf" as F1's output which can be obtained when you replace the Inf by theta_lim[2] and -Inf by theta_lim[1] (at lines no. 278 and 280 respectively, in fscores.internal code. This is the only change I want to make.

After using fixInNamespace("fscores.internal", "mirt")

A window appeared containing :

function (object, ...) 
standardGeneric("fscores.internal")

(fixInNamespace was not helpful)

After using

myfscores.internal <- function() { ... }
assignInNamespace("fscores.internal", myfscores.internal, "mirt")

I got following error, when I ran

fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))



## Error in .local(object, ...) : could not find function "ExtractGroupPars"

Then I compiled ExtractGroupPars function separately.

then the out was

> fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))
Error in .local(object, ...) : could not find function "rotateLambdas"

After this I compiled rotateLambdas function

and I ran

fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))

Then I got following error:

Error in matrix(rep(sqrt(1 - h2), ncol(F)), ncol = ncol(F)) : 
  non-numeric matrix extent
Called from: .rs.breakOnError(TRUE)

Don't know what to do after this.

  • 2
    `fscores.internal` is pure `R`-code and easily accessed for editing from the source tarball. That said, I strongly suggest **against** mucking with an internal method. If there is an error in the package code, notify the maintainer. If you just want to do something different, then write a wrapper or helper function to modify the output of `fscores` . Internal functions are not meant to be mucked with. – Carl Witthoft Oct 15 '14 at 12:14
  • And you used `assignInNamespace` or `fixInNamespace`? And what happened after you attempted to rerun the code after modifying the function? – Thomas Oct 16 '14 at 09:06
  • @Thomas, I have edited question further, please add your suggestions. –  Oct 16 '14 at 09:54

1 Answers1

0

You can use assignInNamespace to rewrite a function inside a package namespace. In this case you would define a new function and assign into the mirt namespace:

myfscores.internal <- function() { ... }
assignInNamespace("fscores.internal", myfscores.internal, "mirt")

Note: Behavior might be somewhat different if fscores.internal is exported from the package namespace.

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • thank your for your reply. Here, I still need to create a new function; isn't there any better way? –  Oct 15 '14 at 11:44
  • 1
    Why is that a problem for you? You can always `rm("myfscores.internal")` to clean up your workspace. – Thomas Oct 15 '14 at 11:45
  • now the problem is that there are few more functions which are used in fscores.internal; now I think the new function myfscores.internal is not recognizing those functions which were used already there in package source. –  Oct 15 '14 at 12:00
  • I don't think it's a problem, but you may need to reference those functions explicitly with something like `mirt:::otherfunction(...)`, etc. rather than just `otherfunction(...)`. – Thomas Oct 15 '14 at 12:51
  • 1
    issue is not solved yet.When I did this: myfscores.internal <- function(){...} assignInNamespace("fscores.internal", myfscores.internal, "mirt") , there was an error showing mirt:::ExtractGroupPars not found, then I compiled this function again an error. I even tried with a wrapper function then also it was throwing error, Where am going wrong? what I have tried: library(mirt) data(LSAT7) LSAT7=expand.table(LSAT7) mod=mirt(LSAT7, 1,itemtype = '3PL') fscores(object = mod,method = "ML",response.pattern= c(1,NA,NA,NA,NA),theta_lim = c(-4, 4)) –  Oct 16 '14 at 05:26
  • Item.1 Item.2 Item.3 Item.4 Item.5 F1 SE_F1 [1,] 1 NA NA NA NA Inf NA This is the final output of the function. I need "theta_lim[2]" instead of "Inf" as F1's output which can be obtained when you replace the Inf by theta_lim[2] and -Inf by theta_lim[1] (at lines no. 278 and 280 respectively, in fscores.internal code. This is the only change I want to make. –  Oct 16 '14 at 05:31
  • @Irri Try editing those details into your question. It's not clear what you're doing based on the unformatted text in your comments. – Thomas Oct 16 '14 at 05:43
  • I have edited my question, please have a look. –  Oct 16 '14 at 06:01