2017-09-01 6 views
1

J'utilise R pour ajuster les données sur une courbe logarithmique avec l'équation:Utiliser nls et ggplot2 pour ajuster une courbe logarithmique aux données

y = a * log (b * x)

Mes données ressemble ceci:

#Creating example data 
pre <- c(946116, 1243227, 1259646, 1434124, 1575268, 2192526, 3252832, 6076519) 
post <- c(907355, 1553586, 1684253, 2592938, 1919173, 1702644,3173743, 3654198) 
data <- data.frame(pre,post) 

#Plotting data 
    ggplot(data, aes(x=pre, y=post))+ 
    geom_point() 

Example plot

Mais lorsque je tente d'ajuster une courbe logarithmique en utilisant geom_smooth, je reçois une erreur.

# Fitting logarithmic curve 
ggplot(data, aes(x=pre, y=post))+ 
    geom_point()+ 
    geom_smooth(method="nls", se=FALSE, 
       method.args=list(formula=y~a*log(b*x), 
           start=c(a=100, b=2))) 

Les messages d'avertissement:

1: In log(b * x) : NaNs produced 
2: Computation failed in `stat_smooth()`: 
Missing value or an infinity produced when evaluating the model 

Je reçois des problèmes similaires lorsque je tente de créer un modèle logarithmique nls, sans utiliser ggplot

model <- nls(data=data, 
      formula=y~a*log(b*x), 
      start=list(a=100, b=2)) 

Messages d'avertissement:

Error in numericDeriv(form[[3L]], names(ind), env) : 
    Missing value or an infinity produced when evaluating the model 
In addition: Warning messages: 
1: In min(x) : no non-missing arguments to min; returning Inf 
2: In max(x) : no non-missing arguments to max; returning -Inf 
3: In log(b * x) : NaNs produced 

Comme quelqu'un qui est nouveau à R, je ne comprends pas très bien ce que les messages d'erreur essayent de me dire. Je sais que je dois changer comment je spécifie les conditions de départ, mais je ne sais pas comment.

Répondre

2

Essayez ceci:

ggplot(data, aes(x=pre, y=post))+ 
    geom_point()+ 
    geom_smooth(method="nls", se=FALSE, formula=y~a*log(x)+k, 
       method.args=list(start=c(a=1, k=1))) 

enter image description here

Notez que c'est essentiellement la même formule, mais maintenant k = a * log(b):

a * log(b * x) = a * {log(b) + log(x)} = a * log(x) + a * log(b) = a * log(x) + k

0

Je vois quelques problèmes dans votre nls appel. 1) Vous utilisez les variables x et et, lorsque ces variables n'existent pas. Ils doivent être pré et après. 2) La taille des nombres donne nls problème. Cela aide si vous les divisez par 1,000,000.

pre <- c(946116, 1243227, 1259646, 1434124, 1575268, 2192526, 3252832, 6076519) 
post <- c(907355, 1553586, 1684253, 2592938, 1919173, 1702644,3173743, 3654198) 

pre = pre/1000000 
post = post/1000000 

data <- data.frame(pre,post) 

model <- nls(data=data, 
      formula=post~a*log(b*pre), 
      start=list(a=1, b=1)) 

summary(model) 

Mais comme indiqué dans la réponse précédente, en changeant la forme de l'équation aidera sans avoir besoin de modifier l'échelle des données.

pre <- c(946116, 1243227, 1259646, 1434124, 1575268, 2192526, 3252832, 6076519) 
post <- c(907355, 1553586, 1684253, 2592938, 1919173, 1702644,3173743, 3654198) 

data <- data.frame(pre,post) 

model <- nls(data=data, 
      formula=post~a*log(pre)+b, 
      start=list(a=1, b=0)) 

summary(model)