0

I am new to R and trying to understand this function call below:

mle_mos <- function (n, m1, m2, x, x1, x2, initial, iters)  
    mvnewton (function (p) log_likelihood_gradient(p,n,m1,m2,x,x1,x2),   
            function (p) -fisher_information(p,n,m1,m2,x,x1,x2),   
            initial, iters)  


mvnewton <- function (f, g, x, n)  
{  
    print(f)
    if (n < 1)  
    stop("invalid number of iterations")  
  
    for (i in 1:n) {  
        cat("\nAt iteration",i,":\n\n")  
        cat("x =",x,"\n")  
        cat("f(x) =",f(x),"\n")  
        cat("g(x) =\n")  
        print(g(x));  
        x <- x - solve(g(x),f(x))  # computes inverse of g(x) times   
        f(x)  
    }  
    x  
}  

mvnnewton takes
(function (p) log_likelihood_gradient(p,n,m1,m2,x,x1,x2)
as the first parameter. Where is p getting it's value from? Where is it defined? Can someone explain where p is getting a value to be passed into log_likelihood_gradient as a parameter? I have been googling for the past 2 days and reading up a lot of stuff but am still not clear that I understand this properly.

3
  • Your code is not clear. Please show a small reproducible example Commented Feb 20, 2022 at 16:31
  • function (p) log_likelihood_gradient(p,n,m1,m2,x,x1,x2) is a definition of a function. It will get the value from the mvnewton function, for which it is an argument. Commented Feb 20, 2022 at 16:32
  • updated code, thanks user2554330, but I still don't understand Commented Feb 20, 2022 at 17:15

1 Answer 1

1

Arguments

If we have a function fun

fun <- function(z) z+1

and then we call it

fun(1)

1 is said to be the actual argument and z is said to be the formal argument. The formal argument takes on the value of the actual argument so z takes on the value 1. (There are some technicalities which we have glossed over but these do not affect the situation in the question.)

mvnewton

When mvnewton is called the caller passes to it four actual arguments:

function (p) log_likelihood_gradient(p,n,m1,m2,x,x1,x2)

function (p) -fisher_information(p,n,m1,m2,x,x1,x2)

initial

iters

Note that the first two arguments are functions. It does not pass the result of calling the functions but passes the functions themselves.

Now within mvnewton these 4 actual arguments correspond to the formal arguments f, g, x and n so when mvnewton refers to f it is really referring to

function (p) log_likelihood_gradient(p,n,m1,m2,x,x1,x2)

Also within mvnewton the function f is called with actual argument x and x corresponds to formal argument p within f (and similarly for g).

Example

For example, suppose we call

f <- function(x) x^2
g <- function(x) 2*x
mvnewton(f, g, 1, 20)  

Then x is 1 and the first time f(x) and g(x) are called within mvnewton they are called as f(1) and g(1) and within f and g the formal argument p takes the value 1. mvnewton then updates x to a new value, calls f and g again with the new value of x and so on.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the difference between the p in function(p) and log_likelihood_gradient(p,n,m1,m2,x,x1,x2)
In function (p) log_likelihood_gradient(p,n,m1,m2,x,x1,x2) the first p is a formal argument and it is passed as an actual argument in the call to log_likelihood_gradient so log_likelihood_gradient winds up using whatever value was passed to function (p) log_likelihood_gradient(p,n,m1,m2,x,x1,x2) when the latter is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.