nls fit function with summation

Hi everyone,

I've been having a hard time trying to do the following and would really appreciate some guidance:

Let's assume I have a function f given by the formula below that I want to use with nls to perform a non-linear regression analysis:

image

where P_i (t) is a non-linear function (in my case an exponential) with parameters a, b and c that I want to know from the regression.

How can I implement the summation to perform the regression analysis? I'm using the nls package but I'm open to change if there is a better one for this use case that I am not aware of.

Your data must be in Matrix form and use Matrix Multiplications. Suppose a data frame of 2 predictors/regressors and the parameters 'a' as a constant and coefficients B [b1 and b2] since we have 2 regressors. Then, you go smoothly as follows;

predic <- data.frame( 'Model_01' = rnorm(50, mean = 4.5, sd = 1.5 ),
                      'Model_02' = rnorm(50, mean = 3.5, sd = 1.2 ) )
predic2Mat <- as.matrix(predic, byrow = F)
predic2Mat <- cbind(rep(1, nrow(predic2Mat)), predic2Mat) # We add 1 for mutrix multiplications

pars <- c('a' = 2, 'b1' = 3.5, 'b2' = 1.8) # a = constant, b1 & b2 are coefficients from your model 
 
new_pred <- predic2Mat %*% pars  # Get the new predictions

Created on 2022-05-24 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.