0

I have the following function:

enter image description here

Of this function, the parameter R is a constant with a value of 22.5. I want to estimate parameters A and B using nonlinear regression (nls() function). I made a few attempts, but all were unsuccessful. I'm not very familiar with this type of operations in R, so I would like your help.

Additionally, if possible, I would also like to plot this function using ggplot2.

# Initial data

x <- c(0, 60, 90, 120, 180, 240)
y <- c(0, 0.967676, 1.290101, 1.327099, 1.272404, 1.354246)
R <- 22.5

df <- data.frame(x, y)

f <- function(x) (1/(n^2))*exp((-B*(n^2)*(pi^2)*x)/(R^2))

# First try

nls(formula = y ~ A*(1-(6/(pi^2))*sum(f, seq(1, Inf, 1))),
    data = df,
    start = list(A = 1,
                 B = 0.7))

Error in seq.default(1, Inf, 1) : 'to' must be a finite number

# Second try

nls(formula = y ~ A*(1-(6/(pi^2))*integrate(f, 1, Inf)),
    data = df,
    start = list(A = 1,
                 B = 0.7))

Error in f(x, ...) : object 'n' not found
Daniel Valencia C.
  • 2,159
  • 2
  • 19
  • 38

1 Answers1

2

You can use a finite sum approximation. Using 25 terms:

f <- function(x, B, n = 1:25) sum((1/(n^2))*exp((-B*(n^2)*(pi^2)*x)/(R^2)))
fm <- nls(formula = y ~ cbind(A = (1-(6/pi^2))* Vectorize(f)(x, B)),
    data = df,
    start = list(B = 0.7),
    alg = "plinear")
fm

giving:

Nonlinear regression model
  model: y ~ cbind(A = (1 - (6/pi^2)) * Vectorize(f)(x, B))
   data: df
       B   .lin.A 
-0.00169  1.39214 
 residual sum-of-squares: 1.054

Number of iterations to convergence: 12 
Achieved convergence tolerance: 9.314e-06

The model does not seem to fit the data very well (solid line in graph below); however, a logistic model seems to work well (dashed line).

fm2 <- nls(y ~ SSlogis(x, Asym, xmid, scal), df)

plot(y ~ x, df)
lines(fitted(fm) ~ x, df)
lines(fitted(fm2) ~ x, df, lty = 2)
legend("bottomright", c("fm", "fm2"), lty = 1:2)

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341