Sine curve fit using lm and nls in R
This is because the NA
values are removed from the data to be fit (and your data has quite a few of them); hence, when you plot fit.lm$fitted
the plot method is interpreting the index of that series as the 'x' values to plot it against.
Try this [note how I've changed variable names to prevent conflicts with the functions time
and data
(read this post)]:
Data <- read.table(file="900days.txt", header=TRUE, sep="")Time <- Data$time temperature <- Data$temperaturexc<-cos(2*pi*Time/366)xs<-sin(2*pi*Time/366)fit.lm <- lm(temperature~xc+xs)# access the fitted series (for plotting)fit <- fitted(fit.lm) # find predictions for original time seriespred <- predict(fit.lm, newdata=data.frame(Time=Time)) plot(temperature ~ Time, data= Data, xlim=c(1, 900))lines(fit, col="red")lines(Time, pred, col="blue")
This gives me:
Which is probably what you were hoping for.
How about choosing an X and an Y while doing your line plot instead of just choosing the Y.
plot(time,predict(fit.nls),type="l", col="red", xlim=c(1, 900), pch=19, ann=FALSE, xaxt="n",yaxt="n")
Also both lm
and nls
just give you the fitted points. So you must estimate the rest of the points in order to make a curve, a line plot. Since you are with nls
and lm
, perhaps the function predict
maybe useful.
Not sure if this might help - I get a similar fit using sine only:
y = amplitude * sin(pi * (x - center) / width) + Offsetamplitude = 2.0009690806953033E+00center = -2.5813588834888215E+01width = 1.8077550471975817E+02Offset = 2.6872265116104828E+01Fitting target of lowest sum of squared absolute error = 3.6755174406241423E+01Degrees of freedom (error): 90Degrees of freedom (regression): 3Chi-squared: 36.7551744062R-squared: 0.816419142696R-squared adjusted: 0.810299780786Model F-statistic: 133.415731033Model F-statistic p-value: 1.11022302463e-16Model log-likelihood: -89.2464811027AIC: 1.98396768304BIC: 2.09219299292Root Mean Squared Error (RMSE): 0.625309918107amplitude = 2.0009690806953033E+00 std err squared: 1.03828E-02 t-stat: 1.96374E+01 p-stat: 0.00000E+00 95% confidence intervals: [1.79853E+00, 2.20340E+00]center = -2.5813588834888215E+01 std err squared: 2.98349E+01 t-stat: -4.72592E+00 p-stat: 8.41245E-06 95% confidence intervals: [-3.66651E+01, -1.49621E+01]width = 1.8077550471975817E+02 std err squared: 3.54835E+00 t-stat: 9.59680E+01 p-stat: 0.00000E+00 95% confidence intervals: [1.77033E+02, 1.84518E+02]Offset = 2.6872265116104828E+01 std err squared: 5.15458E-03 t-stat: 3.74289E+02 p-stat: 0.00000E+00 95% confidence intervals: [2.67296E+01, 2.70149E+01]Coefficient Covariance Matrix[ 0.02542366 0.01786683 -0.05016085 -0.00652111][ 1.78668314e-02 7.30548346e+01 -2.18160818e+01 1.24965136e-01][ -5.01608451e-02 -2.18160818e+01 8.68860810e+00 -1.27401806e-02][-0.00652111 0.12496514 -0.01274018 0.0126217 ]
James Phillipszunzun@zunzun.com