I'm hereby to ask some questions on the model fitting using nls function. My Excel spreadsheet for Sheet 1, Sheet 2 and Sheet 3 consists of 75 variables. First, I fit the model with a modified formula using nls function to obtain my parameter values. After obtaining the parameter values, I insert it into my 'prevalence' to obtain my goal which is to estimate the unknown parameters of P, Q and R. However, the results comes with some mismatch dimension where the unknown parameter values does not fit into the length of the 75 variables. I wish to get some suggestions to encounter this problem. Thank you.
Here is the code as stated below:
#Install Package
library(readxl)
library(MASS)
library(nlstools)
#Read Excel Spreadsheet
Sheet1 <- read_excel("Modelling (1).xlsx", sheet = "Stroke (Male)")
Sheet2 <- read_excel("Modelling (1).xlsx", sheet = "HA (Male)")
Sheet3 <- read_excel("Modelling (1).xlsx", sheet = "Prevalence Rate (Stroke Male)")
#Parameter values obtained from modelling after using nls function for Sheet1 and Sheet2
a1 <- (exp(-1.377957e+01)/1.284528e-01)*(exp(1.284528e-01*20))*(1-(1+0)*exp(1.284528e-01*1))
a2 <- (exp(-1.377957e+01)/1.284528e-01)*(0*exp(1.284528e-01*(20+(1/2))))*(1-1.284528e-01*(1/2))
a <- exp(a1+a2)
b1 <- (-1*exp(-13.684962266)/0.117514215)*(exp(0.117514215*(20+1)))
b2 <- (exp(-13.684962266)/0.117514215)*(exp(0.117514215*(20+(1/2))))*(1-(1/2)*0.117514215)
b <- exp(b1+b2)
c <- exp(-1*0.005378528)
d1 <- -1.377957e+01*0*exp(1.284528e-01*(20+(1/2)))
d2 <- exp(-13.684962266)*exp(0.117514215*(20+(1/2)))
d3 <- -1*6.808971e-04+0.005378528
d <- d1+d2+d3
g1 <- (-1*exp(-14.92307280)/0.13325161)*(exp(0.13325161*(20+1)))
g2 <- (exp(-14.92307280)/0.13325161)*(exp(0.13325161*(20+(1/2))))*(1-(1/2)*0.13325161)
g <- exp(g1+g2)
h <- exp(-1*0.01204073)
i2 <- exp(-14.92307280)*exp(0.13325161*(20+(1/2)))
i3 <- -1*6.808971e-04+0.01204073
i <- d1+i2+i3
#Model Fitting using nls function
prevalence <- nls(Prevalence ~ (P*C*(1-exp(-1*(P+Q+R+E)))/(P+Q+R+E))/((P*C*(1-exp(-1* (P+Q+R+E)))/(P+Q+R+E))+Q*a*g*h*((exp((-P-Q-R+i)*1)-1)/(-P-Q-R+i)),data=Sheet5,start=list(P=0.01,Q=0.01,R=0.01),trace=TRUE)
Results
Error in qr(.swts * gr) : dims [product 3] do not match the length of object [75]*