简体   繁体   中英

Nonlinear regression in R with dummy variables

I need to perform three non-linear regression with the following formulas:

fire_vertical<-nls(R~0.23-exp(A*lnNt+C)+d*fire,start=list(A=-0.33,C=0.45,d=-0.001),trace=T)

fire_lateral<-nls(R~0.23-exp((A*lnNt)+(C+d*fire)),start=list(A=-0.33,C=0.45,d=-0.001),trace=T)

fire_nonlinear<-nls(R~0.23-exp((A+d*fire)*lnNt+C),start=list(A=-0.33,C=0.45,d=-0.001),trace=T)

I have the following data: R<-c(0.02,0.00,-0.06,0.11,0.06,0.00,-0.05,-0.06,0.02,-0.26,0.00,0.07,-0.07,0.23,0.06,-0.14,-0.04,0.09,-0.09,0.09,-0.02)

lnNt<-c(6.14,6.14,5.76,6.42,6.81,6.81,6.49,6.14,6.24,4.81,4.81,5.14,4.81,6.03,6.42,5.59,5.39,5.90,5.39,5.90,5.76)

fire<-c("before","before","before","before","before","before","before","afterone","afterone","afterone","afterone","afterone","afterone","afterone","aftertwo","aftertwo","aftertwo","aftertwo","aftertwo","aftertwo","aftertwo")

"lnNt" and "fire" are my independent variables. lnNt are continuous data, while "fire" is a categorical variable with three levels:"before", "afterone" and "aftertwo".

I need to transform my categorical variable ("fire") into a dummy variable to run the model. I'm not able to run the model with the dummy variable.

Use the gnls function from package nlme. It allows you to model parameters depending on a categorical variable (using the usual treatment contrasts by default).

DF <- data.frame(R, lnNt, fire, stringsAsFactors = TRUE)

fire_model<-nls(R~0.23-exp(A*lnNt+C)+d,
                start=list(A=-0.33,C=0.45,d=-0.001),trace=T, data = DF)
coef(fire_model)
#         A          C          d 
#-1.1367053  3.6708032 -0.1673437 

library(nlme)
fire_model_2 <- gnls(R~0.23-exp(A*lnNt+C)+d,params = list(A + C ~ 1, d ~ fire), 
                     start=c(coef(fire_model), rep(0, length(levels(DF$fire))- 1)), 
                     data = DF)

summary(fire_model_2)
# Generalized nonlinear least squares fit
# Model: R ~ 0.23 - exp(A * lnNt + C) + d 
# Data: DF 
#      AIC       BIC  logLik
# -33.3584 -27.09127 22.6792

# Coefficients:
#                    Value Std.Error    t-value p-value
# A              -1.247828  1.652336 -0.7551907  0.4611
# C               4.558928  7.565058  0.6026296  0.5552
# d.(Intercept)  -0.097251  0.128043 -0.7595203  0.4586
# d.fireaftertwo -0.062324  0.062210 -1.0018480  0.3313
# d.firebefore   -0.083946  0.064853 -1.2944170  0.2139
# 
# Correlation: 
#                A      C      d.(In) d.frft
# C              -0.999                     
# d.(Intercept)   0.864 -0.842              
# d.fireaftertwo  0.435 -0.454  0.050       
# d.firebefore   -0.148  0.117 -0.538  0.492
# 
# Standardized residuals:
#         Min          Q1         Med          Q3         Max 
# -1.66299973 -0.48743154  0.04367184  0.85175935  1.58042481 

In this example we can clearly see that fire does not appear to have a significant impact.

fire_model <- gnls(R~0.23-exp(A*lnNt+C)+d,
                     start=coef(fire_model), data = DF)

anova(fire_model, fire_model_2)
#             Model df       AIC       BIC   logLik   Test  L.Ratio p-value
#fire_model       1  4 -35.05862 -30.88053 21.52931                        
#fire_model_2     2  6 -33.35840 -27.09127 22.67920 1 vs 2 2.299789  0.3167

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM