s_lohengrin
s_lohengrin

Reputation: 23

Getting p-value via nls in r

I'm a complete novice in R and I'm trying to do a non-linear least squares fit to some data. The following (SC4 and t are my data columns) seems to work:

fit = nls(SC4 ~ fin+(inc-fin)*exp(-t/T), data=asc4, start=c(fin=0.75,inc=0.55,T=150.0))

The "summary(fit)" command produces an output that doesn't include a p-value, which is ultimately, aside from the fitted parameters, what I'm trying to get. The parameters I get look sensible.

Formula: SC4 ~ fin + (inc - fin) * exp(-t/T) 

Parameters:
Estimate Std. Error t value Pr(>|t|)


fin  0.73703    0.02065  35.683   <2e-16 ***
inc  0.55671    0.02206  25.236   <2e-16 ***
T   51.48446   21.25343   2.422   0.0224 *  

--- Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.04988 on 27 degrees of freedom
Number of iterations to convergence: 8
Achieved convergence tolerance: 4.114e-06

So is there any way to get a p-value? I'd be happy to use another command other than nls if that will do the job. In fact, I'd be happy to use gnuplot for example if there's some way to get a p-value from it (in fact gnuplot is what I normally use for graphics).

PS I'm looking for a p-value for the overall fit, rather than for individual coefficients.

Upvotes: 2

Views: 4197

Answers (1)

kolonel
kolonel

Reputation: 1422

The way to do this in R is you have to use the anova function to compute the fitted values of your current model and then fit your model with less variables, and then use the function anova(new_model,previous_model). The computed F-score will be closer to one if you cannot reject the null that the parameters for the variables you have removed are equal to zero. The summary function when doing the standard linear regression will usually do this for you automatically.

So for example this is how you would do it for the standard linear regression:

> x = rnorm(100)
> y=rnorm(100)

> reg = lm(y~x)
> summary(reg)

Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.3869 -0.6815 -0.1137  0.7431  2.5939 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)  
(Intercept) -0.002802   0.105554  -0.027   0.9789  
x           -0.182983   0.104260  -1.755   0.0824 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.056 on 98 degrees of freedom
Multiple R-squared:  0.03047,   Adjusted R-squared:  0.02058 
F-statistic:  3.08 on 1 and 98 DF,  p-value: 0.08237

But then if you use anova you should get the same F-score:

> anova(reg)
Analysis of Variance Table

Response: y
          Df  Sum Sq Mean Sq F value  Pr(>F)  
x          1   3.432  3.4318  3.0802 0.08237 .
Residuals 98 109.186  1.1141                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Upvotes: 1

Related Questions