Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206531

R function with dynamic parameters

$
0
0

1. Background

I want to optimize my parameters in likelihood function. The likelihood function is a function about parameters given data we collected.

The number of parameter is related to model and data. In my model, I have fixed number of parameters. Imagine that if you do a linear regression, the number of fixed parameters is 1, i.e. interception.

However, the data shape is unfixed so the parameter of covariates is dynamic. In linear regression tasks, if you input a dataframe with n columns, you will have n(dynamic) + 1(fixed) parameter to estimate!

2. Question

How to deal with this kind of dynamic parameter in programming? If I have 100 parameter to estimate, type them from scratch is unrealistic!

In python, I know that a function can have func(*args,**kwargs) to get many variables. But I can't know how to do it here: given a data with n cols, pass n + 1(dynamic) parameter to my function body and then estimate them elegantly!

I wanna do my optimization easily and elegent like fit a regression. Any method using R or python is welcome!

Here is an example to calculate MLE of multivariate normal distribution mean(data has more than 1 columns), in R:

Code:

### 1 dimensional data
input_data_c1 = data.frame(x = rnorm(1000,mean = 1,sd = 1))

myfun_c1 <- function(x){

  likelihood <- function(mu){
    value = - sum((x - mu)^2)
    return(-value)
  }
  return(optim(par = 0,fn = likelihood,method = 'L-BFGS-B',
               lower = -20, upper = 20)$par)
} 
myfun_c1(input_data_c1)

Output:

[1] 0.9764649

Code:

### 2 dimensional data
input_data_c2 = data.frame(x1 = rnorm(1000,mean = 1,sd = 1),
                        x2 = rnorm(1000,mean = 3,sd = 1))
myfun_c2 <- function(x){

  likelihood <- function(mu){
    # I have to define my function with the shape of input data!
    # If I have a dataframe with 100 columnss, my hands will be broken...
    # Can we do it automatically? 
    value = - sum((x[,1] - mu[1])^2) - sum((x[,2] - mu[2])^2) 
    return(-value)
  }
  return(optim(par = rep(0,2),fn = likelihood,method = 'L-BFGS-B',
               lower = rep(-20,2), upper = rep(20,2))$par)
}
myfun_c2(input_data_c2)

Output

[1] 0.9616378 3.0162921

3. My stupid attempts:

My attempts actually works, but I think they are too trivial and without scalability.

There is a related question in python

template = textwrap.dedent("""
     def func(variable, {fixed}):
          {variable} = variable
            return 4*(b-a)**2 + 5*(c-d)**2
        """
variable = set(('a', 'b', 'c', 'd')).difference(fixed)

I think the {variable} step makes code more scalable. I wonder whether we can do it in R with something like assign and eval.

Code:

### use for loop
myfun_stupid1 <- function(x){

  ncols = dim(x)[2]

  likelihood <- function(mu){
    # I have to difine my function with the shape of input data!
    # If I have a dataframe with 100 columnss, my hands will be broken...
    # Can we do it automatically? 
    value = 0
    for (col_num in 1:ncols)
      value = value + sum((x[,col_num] - mu[col_num])^2) 
    return(value)
  }
  return(optim(par = rep(0,ncols),fn = likelihood,method = 'L-BFGS-B',
               lower = rep(-20,ncols), upper = rep(20,ncols))$par)
}  
myfun_stupid1(input_data_c2)


### use vectorlization 
myfun_stupid2 <- function(x){

  ncols = dim(x)[2]
  x <- as.matrix(x)

  likelihood <- function(mu){
    # I have to difine my function with the shape of input data!
    # If I have a dataframe with 100 columnss, my hands will be broken...
    # Can we do it automatically? 
    x_minus = sweep(x, MARGIN = 2, mu)
    # see sweep(x = matrix(1:12,6,2),MARGIN = 2,c(1,2)), matrix minus a vector
    value <- sum(diag(t(x_minus) %*%  x_minus))
    return(value)
  }

  return(optim(par = rep(0,ncols),fn = likelihood,method = 'L-BFGS-B',
               lower = rep(-20,ncols), upper = rep(20,ncols))$par)
}
myfun_stupid2(input_data_c2)

Viewing all articles
Browse latest Browse all 206531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>