0

I have to use the code bellow but I don't completely understand how it works. Why it won't work if I change du.4 by du.f and then use the f when calling the function? For some reason it only works with numbers and I do not undarstand why.

This is the error that it is giving in the case of du.f

Error in paste("Meth1=", nr, ".ps", sep = "") : object 'f' not found
du.4 <- function(u,v,a){(exp(a)*(-1+exp(a*v)))/(-exp(a)+exp(a+a*u)-exp(a*(u+v))+exp(a+a*v))}

plotmeth1 <- function(data1,data2,alpha,nr) {
     psfile <-paste("Meth1=",nr,".ps",sep="")
     diffmethod <-paste("du.",nr,sep="")
     title=paste("Family",nr)
     alphavalue <-paste("alpha=",round(alpha,digits=3),sep="")
     #message=c("no message")
     postscript(psfile)
     data3<-sort(eval(call(diffmethod,data1,data2,alpha)))
     diffdata <-data3[!is.na(data3)]
     #if(length(data3)>length(diffdata))
     #{message=paste("Family ",nr,"contains NA!")}
     tq <-((1:length(diffdata))/(length(diffdata)+1))
     plot(diffdata,tq,main=title,xlab="C1[F(x),G(y)]",ylab="U(0,1)",type="l")
     legend(0.6,0.3,c(alphavalue))
     abline(0,1)
     #dev.off()
 }
Gabriela M
  • 57
  • 4
  • I don't understand the question. What do you mean by "using f when calling its function"? Does that mean that you are trying to evaluate `function(data1,data2,alpha,f)` without actually defining `f`? If so, why should that work? If you define a function called `du.f` then you can't refer to that function as `f`. If that isn't the issue, please provide a [mcve]. See [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/4996248) for what this would mean in R. – John Coleman Jun 13 '20 at 14:21
  • I mean that if I define it like `du.f ` then `plotmeth1 (data1, data2, alpha, f) ` is not working. data1, data2 and aplha can be anything. – Gabriela M Jun 13 '20 at 14:24
  • 1
    If you define a function called `du.f` then it is called `du.f`, not `f`. If you want to refer to the function as `f`, don't use another name when defining it. Just use `f`. A dot in an R function definition is typically just another character. You wouldn't expect to be able to refer to `durf` as `f`. The case with `du.f` is no different. See [this question](https://stackoverflow.com/q/7526467/4996248) about the dot in R identifiers. – John Coleman Jun 13 '20 at 14:26
  • 1
    Gabriela, are you coming to R from python or another programming language? Do you have an object in the environment named `du`? In other languages that might mean something, but in R typically not. Since you had `du.4` before, were you calling that function as anything other than `du.4`? If you define `du.4`, "of course" `4` by itself works, it is always good in R and most languages as it is a literal number. – r2evans Jun 13 '20 at 14:45
  • Actually that is the only definition I have, and what I do not know is why when I call it `du.4` then it works just by `4` but when I call it `du.f` it does not work by `f`. And how could I do so it would work if I define it by `du.f`. And yes, I haven't worked with R a lot. – Gabriela M Jun 13 '20 at 15:01
  • But if I do it like that it gives me exactly the plot I need. So I think that the code works in that case. – Gabriela M Jun 13 '20 at 15:10
  • I understand the question now. The difference that you are observing is traceable to the fact that `paste` will coerce numbers to strings, but it won't coerce undefined objects into strings. See my answer as edited. – John Coleman Jun 13 '20 at 15:26

1 Answers1

1

In R, a dot is used as just another character in identifiers. It is often used for clarity but doesn't have a formal function in defining the part after the dot as being in a name-space given by the part of the identifier before the dot. In something like du.f you can't refer to the function by f alone, even if your computation is inside of an environment named du. You can of course define a function named du.4 and then use 4 all by itself, but when you do so you are using the number 4 as just a number and not as a reference to the function. For example, if

du.4 <- function(u,v,a){(exp(a)*(-1+exp(a*v)))/(-exp(a)+exp(a+a*u)-exp(a*(u+v))+exp(a+a*v))}

Then du.4(1,2,3) evaluates to 21.08554 but attempting to use 4(1,2,3) throws the error

Error: attempt to apply non-function

In the case of your code, you are using paste to assemble the function name as a string to be passed to eval. It makes sense to paste the literal number 4 onto the string 'du.' (since the paste will convert 4 to the string '4') but it doesn't make sense to paste an undefined f onto 'du.'. It does, however, make sense to paste the literal string 'f' onto 'du.', so that the function call plotmeth1 (data1, data2, alpha, 'f') will work even though plotmeth1 (data1, data2, alpha, f) will fail.

See this question for more about the use of the dot in R identifiers.

John Coleman
  • 51,337
  • 7
  • 54
  • 119