I need to import two columns (time and temp) worth of data from a .csv file into R and convert it to a time-series and the plot it with a linear regression line. So far this is my approach:
Wintemp <- read.csv("FileName.csv") Wintemp.ts <- as.ts(Wintemp) plot(Wintemp,type="l")
I am able to successfully get a graph of the time series with the code above.
However I am having trouble with plotting the linear regression line. This is the code I have attempted with:
lm(formula = Wintemp) #Gives wrong values as my independent variable is time not temperature.
Call: lm(formula = Wintemp) Coefficients: (Intercept) 635.2 Temp -18.5
Lfit <- 635.3 - 18.5 * Wintemp ts.plot(Wintemp.ts,Lfit, xlab = "time", ylab = "Temp")
This plots a line but it is far from correct.
I am new to R, could you please help with what the simplest way to perform these set of actions would be? Or what are the mistakes I am making?
Thank you, any help is very much appreciated.