LeoB
LeoB

Reputation: 35

unused arguments in R for sample function

i'm trying to prove empirically the non distorsion of an estimator. what i've written:

distorsione = function(nrepl=1e4){

    pop = c(1,2,3,5,6,7,8,9,50)
    var.pop = var(pop)

    dim.camp = 4
    
    sample = function(i){
        camp = sample(pop, dim.camp, replace =T)
        media.camp = mean(camp)
        diff = camp-media.camp
        diff2 = diff^2
        sigma2 = sum(diff2)/dim.camp
        return(sigma2)
    }
    sigmas = mapply(sample, 1:nrepl)
    ev = mean(sigmas)
    print(paste("distorsione: ", abs(ev - var.pop)))
    
}

when it sample i gives me the error

Error in sample(pop, dim.camp) : unused argument (dim.camp, replace=T)

what am I doing wrong?

Upvotes: 0

Views: 985

Answers (2)

andrewb
andrewb

Reputation: 106

Naming your outer function sample is causing problems because it is overwriting the base version of that function. Your new function only takes one argument so that's why it's telling you you have unused arguments.

Try naming your function something else or if you really want it called sample you could change

camp = sample(pop, dim.camp, replace =T)

to

camp = base::sample(pop, dim.camp, replace =T)

Upvotes: 1

Basti
Basti

Reputation: 1763

You are calling the sample function inside your sample function, I believe you wanted to use the former sample function in the base package and transform it, I encourage you to change the name of your new function :

distorsione = function(nrepl=1e4){
  

      pop = c(1,2,3,5,6,7,8,9,50)
      var.pop = var(pop)
      
      dim.camp = 4
      
      sample2 = function(i){
        camp = base::sample(pop, dim.camp, replace =T)
        media.camp = mean(camp)
        diff = camp-media.camp
        diff2 = diff^2
        sigma2 = sum(diff2)/dim.camp
        return(sigma2)
      }
      sigmas = mapply(sample2, 1:nrepl)
      ev = mean(sigmas)
      print(paste("distorsione: ", abs(ev - var.pop)))
    }

Upvotes: 1

Related Questions