0

I am trying to integrate a quadratic linear function for each individual within my study system.

I have already fit a lm for each individual and calculated the intercept, linear coefficient and quadratic coefficient so my data set looks something like this for 75 individuals:

id.x  Intercept       coeff1        coeff2
1     0.1683109    -0.005091914  3.850922e-05
45   -0.5147262     0.015340073  -1.142777e-04

I would like the integrate the area under the curve ((int+x*coeff1+I(x^2)*coeff2) from lower=60.1 to upper=70.37) for each individual.

I have tried using the integrate function written into a for loop:

for( i in 1:nrow( data ) ){
    integrate( function(x)( data$Intercept[i]+data$coeff1[i]*x+data$coeff2[i]*I(x^2) ), lower=60.1, upper=70.37 )
}

As well as by using the sapply function but have not been able to get either to work. Any help with the coding would be greatly appreciated !!

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
ck982
  • 1
  • 2
  • What error messages do you get if any? – Aidan Jan 25 '16 at 00:12
  • I appreciate the response! I solved the problem with the following code: beta.estimates<-setDT(data)[,list(Intercept=lm(hazardratio~burden+I(burden^2))$coefficients[1], coeff1=lm(hazardratio~burden+I(burden^2))$coefficients[2], coeff2=lm(hazardratio~burden+I(burden^2))$coefficients[3]),by=id.x] – ck982 Jan 25 '16 at 23:50

0 Answers0