Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to estimate the best fitting function to a scatter plot in R?

I have scatterplot of two variables, for instance this:

x<-c(0.108,0.111,0.113,0.116,0.118,0.121,0.123,0.126,0.128,0.131,0.133,0.136)

y<-c(-6.908,-6.620,-5.681,-5.165,-4.690,-4.646,-3.979,-3.755,-3.564,-3.558,-3.272,-3.073)

and I would like to find the function that better fits the relation between these two variables.

to be precise I would like to compare the fitting of three models: linear, exponential and logarithmic.

I was thinking about fitting each function to my values, calculate the likelihoods in each case and compare the AIC values.

But I don't really know how or where to start. Any possible help about this would be extremely appreciated.

Thank you very much in advance.

Tina.

like image 443
user18441 Avatar asked Dec 16 '22 13:12

user18441


1 Answers

I would begin by an explantory plots, something like this :

x<-c(0.108,0.111,0.113,0.116,0.118,0.121,0.123,0.126,0.128,0.131,0.133,0.136)
y<-c(-6.908,-6.620,-5.681,-5.165,-4.690,-4.646,-3.979,-3.755,-3.564,-3.558,-3.272,-3.073)
dat <- data.frame(y=y,x=x)
library(latticeExtra)
library(grid)
xyplot(y ~ x,data=dat,par.settings = ggplot2like(),
       panel = function(x,y,...){
         panel.xyplot(x,y,...)
       })+
  layer(panel.smoother(y ~ x, method = "lm"), style =1)+  ## linear
  layer(panel.smoother(y ~ poly(x, 3), method = "lm"), style = 2)+  ## cubic
  layer(panel.smoother(y ~ x, span = 0.9),style=3)  + ### loeess
  layer(panel.smoother(y ~ log(x), method = "lm"), style = 4)  ## log

enter image description here

looks like you need a cubic model.

 summary(lm(y~poly(x,3),data=dat))

Residual standard error: 0.1966 on 8 degrees of freedom
Multiple R-squared: 0.9831, Adjusted R-squared: 0.9767 
F-statistic: 154.8 on 3 and 8 DF,  p-value: 2.013e-07 
like image 178
agstudy Avatar answered Apr 26 '23 23:04

agstudy