nonlinear regression in R with multiple data sets -
i learning r , using non linear regression (which learning). have 2 sets of data (duration of operation on different machines) , able find non linear regression each of these sets. now, find best regression minimise sum of both residual sum-of-squares.
here have :
a <- c(1:5) b <- c(100, 51, 32, 24, 19) c <- c(150, 80, 58, 39, 29) df <- data.frame (a,b,c) f <- b ~ k1/a + k2 g <- c ~ k1/a + k2 n <- nls(f, data = df, start = list(k1=10, k2=10)) p <- nls(g, data = df, start = list(k1=10, k2=10)) n #nonlinear regression model # model: b ~ k1/a + k2 # data: df # k1 k2 #101.595 -1.195 # residual sum-of-squares: 2.619 #number of iterations convergence: 1 #achieved convergence tolerance: 2.568e-07 p #nonlinear regression model # model: c ~ k1/a + k2 # data: df # k1 k2 #148.044 3.593 # residual sum-of-squares: 54.19 #number of iterations convergence: 1 #achieved convergence tolerance: 1.803e-07
k1 , k2 constant (of course) different both sets (b , c), wondering how manage find particular k1 , particular k2 produce 'best' solution both data sets.
hope explanation understandable. otherwise, i'm trying find (at least here) called global non linear regression.
edit : know how can tell r avoid negative values specific parameter. in case, k2 positive.
if want identical parameters, should pool data:
df2 <- data.frame(y=c(df$b,df$c), x=rep(df$a, 2)) p <- nls(y ~ k1/x + k2, data = df2, start = list(k1=10, k2=10), lower = c(0, 0), algorithm = "port") summary(p) # formula: y ~ k1/x + k2 # # parameters: # estimate std. error t value pr(>|t|) # k1 124.819 18.078 6.904 0.000124 *** # k2 1.199 9.781 0.123 0.905439 # --- # signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # residual standard error: 16.59 on 8 degrees of freedom # # algorithm "port", convergence message: both x-convergence , relative convergence (5)
edit:
if want 1 parameter equal , 1 vary, use mixed effects model. however, don't know how specify constraints (i believe not simple task, possibly achieved reparameterization).
library(nlme) library(reshape2) df3 <- melt(df, id.vars="a") r <- nlme(value ~ k1/a + k2, data = df3, start = c(k1=10, k2=10), fixed = k1 + k2 ~1, random = k2 ~ 1|variable) summary(r) # nonlinear mixed-effects model fit maximum likelihood # model: value ~ k1/a + k2 # data: df3 # aic bic loglik # 83.11052 84.32086 -37.55526 # # random effects: # formula: k2 ~ 1 | variable # k2 residual # stddev: 12.49915 7.991013 # # fixed effects: k1 + k2 ~ 1 # value std.error df t-value p-value # k1 124.81916 9.737738 7 12.818086 0.0000 # k2 1.19925 11.198211 7 0.107093 0.9177 # correlation: # k1 # k2 -0.397 # # standardized within-group residuals: # min q1 med q3 max # -1.7520706 -0.5273469 0.2746039 0.5235343 1.4971808 # # number of observations: 10 # number of groups: 2 coef(r) # k1 k2 # b 124.8192 -10.81835 # c 124.8192 13.21684
Comments
Post a Comment