I am trying to write a custom model for gnm, to fit Voigt profiles.
I have defined
library(gnm) ; library(RcppFaddeeva)
VOIGT <- function(x) {
list(predictors=list(Center=1, Sigma=1, Gamma=1, Alfa=1),
variables=list(substitute(x)),
term=function(predLabels, varLabels) {
paste(predLabels[4], "* RcppFaddeeva::Voigt(", varLabels[1], ", ", predLabels[1], ", ", predLabels[2], ", ", predLabels[3], ")" )
},
start=function(theta) {
theta[2] <- 1 ; theta[3] <- 1; theta[4] <- 1
} )
}
class(VOIGT) <- "nonlin"
but trying to use it gives
Error in deriv.default(X[[i]], ...) :
Function 'RcppFaddeeva::Voigt' is not in the derivatives table
Any workarounds? Or does this need extensions to gnm, or some completely other approach?
You can use
nlsto fit the model using least squares.For example, generate some data from a Voigt profile:
Sample
npoints to use as example dataAdd some Gaussian noise to the response
Plot the example data (see below, with final fit)
From the plot, we can see the centre is around 250 and if we approximate the profile by a Gaussian we might guess sigma is around 100 (profile falls to zero around 3 times sigma from the centre). This gives starting values for our parameters (alpha and gamma we can try setting to 1).
Add the fitted model to the plot