D.Mac
D.Mac

Reputation: 13

Smooth interpolation of my data

I'm trying create smooth lines on a plot which include the maxima of the data points. I've searched around a lot and have tinkered with loess() and ksmooth() but I'm yet to make it work.

My best attempt so far has been with ksmooth() but the line doesn't pass through the maximum data point

enter image description here

I'm a chemist, not a statistician, so the methods/descriptions of various smoothing techniques often go over my head. Any suggestions would be really appreciated.

Edit: Just wanted to make a few things clearer. Basically what I'm after is a smoothed version of the following plot with the line passing through the maximum y value.

enter image description here

To generate the plot in the first picture I used the following code:

plot(ChiM~Temp, xlim=c(2,6), ylim=c(0,0.225), lwd=2, pch=16, col='red',subset=(v=='20'), main='Out-of-Phase AC Suscetability Plot', xlab='Temperature (K)', ylab=expression(chi[M]*'" (cm'^3*~'mol'^-1*')'))
setone <- subset(DSM32ac, v=='20') #v=20 is the subset of the data I have provided
attach(setone)
lines(ksmooth(Temp, ChiM, 'normal', bandwidth=0.5), col='red',lwd=2)

I hope this makes things a little clearer. If you need any more information to answer this question just let me know.

Edit 2: I've removed the data since I can't make a neat table. If it's really important I'll try and put it back in.

Upvotes: 1

Views: 2080

Answers (2)

Sandipan Dey
Sandipan Dey

Reputation: 23109

You can try this:

n <- 10 # generate 10 data points
d <- data.frame(x = 1:n, y = rnorm(n))

# with loes smoothing (span parameter controls the degree of smoothing)
library(ggplot2)
ggplot() + geom_point(data=d,aes(x,y), size=5) + 
  geom_smooth(data=d,aes(x,y, colour='span=0.5'), span=0.5, se=FALSE) +
  geom_smooth(data=d,aes(x,y, colour='span=0.6'), span=0.6, se=FALSE) +
  geom_smooth(data=d,aes(x,y, colour='span=0.7'), span=0.7, se=FALSE) +
  geom_smooth(data=d,aes(x,y, colour='span=0.8'), span=0.8, se=FALSE)

enter image description here

# with B-spline curves using lm (degree of polynomial fitted controls the smoothness)
ggplot() +
  geom_point(data=d, aes(x, y), size=5) +
  geom_smooth(data=d, aes(x, y,col='degree=3'), method = "lm", formula = y ~ splines::bs(x, 3), se = FALSE) +
  geom_smooth(data=d, aes(x, y,col='degree=4'), method = "lm", formula = y ~ splines::bs(x, 4), se = FALSE) +
  geom_smooth(data=d, aes(x, y,col='degree=5'), method = "lm", formula = y ~ splines::bs(x, 5), se = FALSE) +
  geom_smooth(data=d, aes(x, y,col='degree=6'), method = "lm", formula = y ~ splines::bs(x, 6), se = FALSE) +
  geom_smooth(data=d, aes(x, y,col='degree=7'), method = "lm", formula = y ~ splines::bs(x, 6), se = FALSE)

enter image description here

# with smooth.spline (spar parameter control smoothness of the fitted curve)
colors <- rainbow(100)
plot(d$x, d$y, pch=19, xlab='x', ylab='y')
i <- 1
for (spar in seq(0.001,1,length=100)) {
  lines(smooth.spline(d$x, d$y, spar=spar, all.knots=TRUE)$y, col=colors[i])
  i <- i + 1
}
points(d$x, d$y, pch=19)

enter image description here

Upvotes: -1

G. Grothendieck
G. Grothendieck

Reputation: 269860

Try this:

y <- c(.07, .12, .17, .11, .04, .02, .01)
x <- seq_along(y)

s <- spline(x, y)

plot(y ~ x)
lines(s)

giving:

screenshot

Upvotes: 2

Related Questions