Help for drawing linear trend with lm or abline.

#1
Hi,
I am a complete newbee in R, and I have a problem with linear trend fitting.

My data is daily financial data for around 4 years. The financial year is 260 days (5 days a week) so the total amount of data is 913.
For the simplicity, the data is only one cloumb with 913 data and with one header.

I can plot the time series data without problem. But when I try to plot linear trend with lm, I can get the error message:
"formula.default(object, env = baseenv()) : invalid formula"

And when I try to plot the linear trend with abline, I get the error message:
"Error: $ operator is invalid for atomic vectors"

My code is below. So can anyone help me draw linear trend with lm or abline? I check the web to see how lm or abline is used. Some examples used "formula" for lm and abline, but I didn't understand what kind of formula i should use.

Code:
SE=read.csv(file="SE1.csv",head=TRUE,sep="")
Price<-SE$Closingprice
Price.SEK<-ts(Price, start=2011)
plot(Price.SEK,main="Stockholm 30 Index from August 2011 to March 2015",lwd=1,col=4)
str(Price.SEK)
lm.01<-lm(Price.SEK)
 

JesperHP

TS Contributor
#2
But when I try to plot linear trend with lm, I can get the error message:
"formula.default(object, env = baseenv()) : invalid formula"
lm() is not for plotting linear models but for fitting linear models.
To fit a linear model you need to give lm() an argument of the type formula
this argument defines the linear model

Code:
y~1
is a model of the data y on a constant
Code:
y ~ x
is model of data y on data x




here is simulation example:

Code:
# Simulate som prices
prices=cumprod(exp(rnorm(10000,sd=0.001)))
plot(prices,type="l")

# Fit linear model
T=length(prices)
time=1:T

model=lm(prices ~time)
abline(model,col="red")

you could google "R model formula syntax"

heres an example of what you then get ...
http://science.nature.nps.gov/im/datamgmt/statistics/r/formulas/
 
#3
Thanks a lot for the help. I solved the problem as below:

Code:
 SE=read.csv(file="SE.csv",head=TRUE,sep=";")

    Date<-SE$Date
    Da<-as.Date(Date,format="%m-%d-%Y")
    Price<-SE$Closingprice
Price.SEK<-ts(Price)
    plot(Price.SEK~Da, SE,xaxt = "n",type="l", main="Stockholm 30 Index from August 2011 to March 2015",lwd=1,col=4)
    axis(1, Da, format(Da,"%m-%Y"),cex.axis = .7)
    model=lm(Price.SEK~Da)
    abline(model, col="red")