In [2]:
library(rminuit2, quiet=TRUE)
In [4]:
#
# simulate model y = a*exp(-x/b) + k
#
x = seq(0, 1, length.out=31)
y.func = function(x, norm, tau, const) norm*exp(-x/tau) + const
In [5]:
#
# simulate data with Gaussian errors for specific model
# the model includes norm and tau but not const, which is a fixed parameter
#
model.par = c(norm=2.3, tau=0.47)
model.extra.const = 4.7
y.err = 0.01
y = do.call(y.func, c(list(x), model.par, const=model.extra.const)) + rnorm(sd=y.err, n=length(x))
In [6]:
# fit model on data, ask to compute Minos errors too
fit.rc = rminuit2_expr(y ~ norm*exp(-x/tau) + const, c(norm=1, tau=10),
data=data.frame(x=x, y=y, y.err=y.err), errors=y.err,
lh="Gaussian", opt="hm", const=model.extra.const)
In [10]:
# chi square / number of degrees of freedom
cat("chi square and number of degrees of freedom")
cbind(chisq=fit.rc$chisq, ndof=fit.rc$ndof)
# fitted parameters and their estimated uncertainties
cat("fit parameters: model, fit result, uncertainty, minos uncertainties\n")
cbind(model.value=model.par, value=fit.rc$par, error=fit.rc$err,
minos_pos=fit.rc$err_minos_pos, minos_neg=fit.rc$err_minos_neg)
# parameters' correlation matrix
cat("fit parameter correlation matrix\n")
fit.rc$cor
No comments:
Post a Comment