0
$\begingroup$

My data is structured like so:

'data.frame': 50 obs. of 3 variables:

  • Project : Factor w/ 2 levels "A","B": 1 1 1 1 1
  • x: int 2 2 2 2 6 4 4 4 6 4 ...
  • y: num 0.622 0.425 0.363 0.344 0.346 ...

I have 'attached' the data and plotted both levels in my scatter plot using:

plot(x,y,pch=as.numeric(Project))
TSF<-x[order(x)]
SD<-y[order(x)]

And fitted a non-linear regression to the data

nls_fit <- nls(SD ~ a - (b*TSF)+ ((c*TSF)^(2)), start = list(a = 0.34, b = 0.017, 
+     c = 0.0003))
lines(TSF, predict(nls_fit), col = "red")

This works well...

But how can I fit this equation to only the factor "A" of the data? I'm very new at this so if you have the time an inclination it would be great if you could describe what any code you write does.

Thanks Kris

$\endgroup$
3
$\begingroup$

If I've understood correctly, there's no need to use non-linear regression for this. You can use linear regression because the model can be written in a way that the parameters appear linearly. If $E$ denotes expected value, your model is $E(SD) = a - b \text{TSF} + c^2 \text{TSF}^2$, which you could write as $E(SD) = \beta_0 + \beta_1 \text{TSF} + \beta_2 \text{TSF}^2$, with $\beta_0=a, \ \beta_1=-b, \ \beta_2 = c^2$.

If the name of your data frame is d, you can fit the model to the full data set using lm.all <- lm(y ~ x + I(x^2), data = d). You can fit the model just using the "A" points using lm.A <- lm(y ~ x + I(x^2), data = subset(d, Project == "A")). Note that the intercept is included by default, and the I means that you want x^2 to be interpreted as "x squared". Also note that there's no need to sort the data.

$\endgroup$
  • $\begingroup$ +1 But please note that subset should only be used in the global environment. See also the warning in the help file. $\endgroup$ – Roland Dec 13 '12 at 10:53
  • $\begingroup$ @Roland Thanks for the comment. I didn't know that. $\endgroup$ – mark999 Dec 13 '12 at 18:10
  • $\begingroup$ Thank you Mark, that is really clear and works well. I wish I had sought help sooner! $\endgroup$ – Kris Brooks Dec 17 '12 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.