I have a dataframe, with
### create sample data
set.seed(1)
x = runif(n = 100, 0, 20)
beta1 = 2
beta2 = 1
beta3 = (1/2)
### Create sample data frame and vector
y = beta1 + beta2*x^(beta3) + rnorm(n = 100, 0, 0.01)
data = as.data.frame(cbind(y,x))
### Fitting the data with nls()-function;
fit1 = nls(
y~vb1(x,beta1,beta2,beta3),
data=data,start=list(beta1 = 0, beta2 = 1, beta3 = 1)
)
where
vb1 = function(x,beta1,beta2,beta3){
beta1 + beta2*x^(beta3)
}
In the end I want to plot the output:
plot(y~x, col = 3)
nlsTracePlot(fit1,vb1(x,beta1,beta2,beta3),legend="bottomright")
However, it gives the following error,
3. stop(gettextf("'%s' is not a function, character or symbol", deparse(FUN)), domain = NA) 2. match.fun(fun) 1. nlsTracePlot(fit1, vb1(x, beta1, beta2, beta3), legend = "bottomright")
Everything works just fine, until I try to plot it with the above function.
Assuming you are using the FSA
package, you will need to pass the fun
argument a raw function. The function needs to be specified in a particular way. See documentation of nlsTracePlot
for more information.
vb1 <- function(x, beta1, beta2, beta3){
if (length(beta1) == 3) {
beta2 <- beta1[2]
beta3 <- beta1[3]
beta1 <- beta1[1]
}
beta1 + beta2 * x^(beta3)
}
plot(y ~ x, col = 3)
nlsTracePlot(fit1, vb1, legend = "bottomright")