I'm new to curve customization, and a few posts on Stackoverflow really helped me.
I tried to fit the sine curve to my data using lm and nls , but both methods show a strange fit, as shown below. Can anyone please indicate where I was wrong. I would suspect something related to time, but could not understand. Access to my data can be obtained from here . 
data <- read.table(file="900days.txt", header=TRUE, sep="") time<-data$time temperature<-data$temperature #lm fitting xc<-cos(2*pi*time/366) xs<-sin(2*pi*time/366) fit.lm<-lm(temperature~xc+xs) summary(fit.lm) plot(temp~time, data=data, xlim=c(1, 900)) par(new=TRUE) plot(fit.lm$fitted, type="l", col="red", xlim=c(1, 900), pch=19, ann=FALSE, xaxt="n", yaxt="n") #nls fitting fit.nls<-nls(temp~C+alpha*sin(W*time+phi), start=list(C=27.63415, alpha=27.886, W=0.0652, phi=14.9286)) summary(fit.nls) plot(fit.nls$fitted, type="l", col="red", xlim=c(1, 900), pch=19, ann=FALSE, xaxt="n", axt="n")
r curve-fitting
Eddie
source share