Noela Sánchez
2009-Sep-17 10:10 UTC
[R] Problems with the commands FUNCTION and DERIV to build a polynomial
Hi all, I need to automate a process in order to prepare a a big loop in the future but I have a problem with the *command function* First I fit a model with lm>model1<-lm(data2[,2]~data2[,1]+I(data2[,1]^2)+I(data2[,1]^3)+I(data2[,1]^4)) I extract the coefficients to build the polynomial. coef<-as.matrix(model1$coefficients) In the next step I need to define the polynomial to derive it. If I write the coefficients manually (writing the numbers by hand) the deriv command works fine! bb<-deriv(~2847.22015 -463.06063*x+ 25.43829*x^2 -0.17896*x^3, namevec="x", function.arg=40) I would like to automate this step by being able to extract the coefficients from the linear model and adding them into the polynomial (and not write them by hand)! But if I build the polynomial with the function(x) command calling the * coef* values, the numeric values are not interpreted, the command function does not read properly the coefficients from the linear model.> fun<-function(x) coef[1]+coef[2]*x+coef[3]*x^2+coef[4]*x^3> funfunction(x) coef[1]+coef[2]*x+coef[3]*x^2+coef[4]*x^3 How can i avoid to write the values of the coefficients by hand?? I need to do this many many times, this is the reason i need to be able to automate the process and then build a loop to repeat it many times with different outputs of a linear model! Somebody can help me? -- Noela Grupo de Recursos Marinos y Pesquerías Universidad de A Coruña [[alternative HTML version deleted]]
Noela Sánchez
2009-Sep-17 11:06 UTC
[R] Problems with the commands FUNCTION and DERIV to build a polynomial
Hi all, I need to automate a process in order to prepare a a big loop in the future but I have a problem with the *command function* First I fit a model with lm>model1<-lm(data2[,2]~data2[,1]+I(data2[,1]^2)+I(data2[,1]^3)+I(data2[,1]^4)) I extract the coefficients to build the polynomial. coef<-as.matrix(model1$coefficients) In the next step I need to define the polynomial to derive it. If I write the coefficients manually (writing the numbers by hand) the deriv command works fine! bb<-deriv(~2847.22015 -463.06063*x+ 25.43829*x^2 -0.17896*x^3, namevec="x", function.arg=40) I would like to automate this step by being able to extract the coefficients from the linear model and adding them into the polynomial (and not write them by hand)! But if I build the polynomial with the function(x) command calling the * coef* values, the numeric values are not interpreted, the command function does not read properly the coefficients from the linear model.> fun<-function(x) coef[1]+coef[2]*x+coef[3]*x^2+coef[4]*x^3> funfunction(x) coef[1]+coef[2]*x+coef[3]*x^2+coef[4]*x^3 How can i avoid to write the values of the coefficients by hand?? I need to do this many many times, this is the reason i need to be able to automate the process and then build a loop to repeat it many times with different outputs of a linear model! Somebody can help me? -- Noela Grupo de Recursos Marinos y Pesquerías Universidad de A Coruña [[alternative HTML version deleted]]