0

My dataset ICM_Color0 has the following structure, where columns are:

Lum Ruido Dist RT.ms Condicion

With 2599 rows.

There are three luminance = [13,19,25];and two types of noise = [1, 2] -> 3x2 = 6 conditions.

Condicion:

Lum   Ruido   Condicion
13     1        1
13     2        2
19     1        3
19     2        4
25     1        5
25     2        6

My model is:

Color0.nls <- nls(RT.ms ~ 312 + K[Condicion]/(Dist^1),     
              data = ICM_Color0, start = list(K = rep(1,6)))



> summary(Color0.nls)

Formula: RT.ms ~ RT0.0 + K[Condicion]/(Dist^n)

Parameters:
   Estimate Std. Error t value Pr(>|t|)    
K1  1.84108    0.03687   49.94   <2e-16 ***
K2  2.04468    0.03708   55.14   <2e-16 ***
K3  1.70841    0.03749   45.58   <2e-16 ***
K4  2.09915    0.03628   57.86   <2e-16 ***
K5  1.62961    0.03626   44.94   <2e-16 ***
K6  2.18235    0.03622   60.26   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 120.5 on 2593 degrees of freedom

Number of iterations to convergence: 1 
Achieved convergence tolerance: 1.711e-08

I need to plot the actual data and parameter estimation.

I already made a general review of the literature but found no examples with a model like mine, where the model depends on the condition variable. Can anyone guide me?

Thanks a lot

1 Answers1

0

It's fairly straightforward to plot the fitted lines from a regression (non-linear or not). I most often do this by using predict to calculate the predicted values from the original data and then plotting those as lines on top of a scatterplot of the data.

You didn't give a reproducible example, so I made some nonlinear data following this answer.

# Create data to fit with non-linear regression
set.seed(16)
x = seq(100)
y = rnorm(200, 50 + 30 * x^(-0.2), 1)
site = rep(c("a", "b"), each = 100)
dat = data.frame(expl = c(x, x), resp = y, site)

Then I fit a nonlinear regression, allowing each parameter to vary by the grouping variable site.

fit1 = nls(resp ~ a[site] + b[site] * expl^(-c[site]), data = dat, 
      start = list(a = c(80, 80), b = c(20, 20),  c = c(.2, .2)))

Now I just add the fitted values to the dataset using predict.nls

dat$pred = predict(fit1)

I plotted this using the ggplot2 package.

ggplot(data = dat, aes(x = expl, y = resp, color = site)) +
    geom_point() +
    geom_line(aes(y = pred))

In this case, where I'm allowing all parameters to vary by site, it looks like you can do all of this in ggplot through geom_smooth. I found a very nice example of this here.

Here is what it would look like with the toy dataset.

ggplot(data = dat, aes(x = expl, y = resp, color = site)) +
    geom_point() +
    geom_smooth(aes(group = site), method = "nls", formula = "y ~ a + b*x^(-c)", 
              start = list(a = 80, b = 20,  c = .2), se = FALSE)
Community
  • 1
  • 1
aosmith
  • 34,856
  • 9
  • 84
  • 118