fnSubset: Call fnFull with variable and fixed parameters

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/fnSubset.R

Description

Combine variable parameters with with fixed parameters and pass to fnFull. Useful for optimizing over a subset of parameters without writing a separate function. Values are combined by name if available. Otherwise, xFull is constructed by position (the default).

Usage

1
fnSubset(x, fnFull, xFixed, xFull=c(x, xFixed), ...) 

Arguments

x

Variable parameters to be passed to fnFull.

fnFull

Function whose first argument has length = length(xFull).

xFixed

Parameter values to be combined with x to construct the first argument for a call to fnFull.

xFull

Prototype initial argument for fnFull.

...

Optional arguments passed to fnFull.

Details

This function first confirms that length(x) + length(xFixed) == length(xFull). Next,

Finally, call fnFull(xFull, ...).

Value

value returned by fnFull

Author(s)

Spencer Graves

See Also

optim dlmMLE maxLik maxNR

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
##
## Example with 'optim'
##
fn <- function(x) (x[2]-2*x[1])^2
# note: true minimum is 0 on line 2*x[1] == x[2]
fullEst <- optim(par=c(1,1), method="BFGS", fn=fn)
fullEst$par
# par = c(0.6, 1.2) at minimum (not convex)

# Fix the last component to 4 
est4 <- optim(par=1, fn=fnSubset, method="BFGS", fnFull=fn, xFixed=4)
est4$par
# now there is a unique minimun x[1] = 2

# Fix the first component
fnSubset(x=1, fnFull=fn, xFixed=c(a=4), xFull=c(a=1, b=2))
# After substitution:  xFull = c(a=4, b=1),
# so fn = (1 - 2*4)^2 = (-7)^2 = 49

est4. <- optim(par=1, fn=fnSubset, method="BFGS",
               fnFull=fn, xFixed=c(a=4), 
               xFull=c(a=1, b=2))
est4.$par
# At optimum: xFull=c(a=4, b=8),
# so fn = (8 - 2*4)^2 = 0

##
## Example with 'maxLik'
##
fn2max <- function(x) -(x[2]-2*x[1])^2
# -> need to have a maximum
max4 <- maxLik(fnSubset, start=1, fnFull=fn2max, xFixed=4)
summary(max4)
# Similar result using fixed parameters in maxNR, called by maxLik 
max4. <- maxLik(fn2max, start=c(1, 4), fixed=2)
summary(max4.)

Example output

Loading required package: miscTools

Please cite the 'maxLik' package as:
Henningsen, Arne and Toomet, Ott (2011). maxLik: A package for maximum likelihood estimation in R. Computational Statistics 26(3), 443-458. DOI 10.1007/s00180-010-0217-1.

If you have questions, suggestions, or comments regarding the 'maxLik' package, please use a forum or 'tracker' at maxLik's R-Forge site:
https://r-forge.r-project.org/projects/maxlik/
[1] 0.6 1.2
[1] 2
 b 
49 
[1] 8
--------------------------------------------
Maximum Likelihood estimation
Newton-Raphson maximisation, 2 iterations
Return code 1: gradient close to zero
Log-Likelihood: -4.173074e-28 
1  free parameters
Estimates:
     Estimate Std. error t value  Pr(> t)    
[1,]   2.0000     0.3536   5.657 1.54e-08 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
--------------------------------------------
--------------------------------------------
Maximum Likelihood estimation
Newton-Raphson maximisation, 2 iterations
Return code 1: gradient close to zero
Log-Likelihood: -4.173074e-28 
1  free parameters
Estimates:
     Estimate Std. error t value  Pr(> t)    
[1,]   2.0000     0.3536   5.657 1.54e-08 ***
[2,]   4.0000     0.0000      NA       NA    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
--------------------------------------------

maxLik documentation built on July 27, 2021, 1:07 a.m.