[R] Problems with "predict" function ii
Rui Barradas
ruipbarradas at sapo.pt
Wed Jan 31 19:30:00 CET 2018
Hello,
First of all, your question is about 'predict' but you include graphic
instructions that have nothing to do with it. They do not hurt, but the
reproducible example should also be minimal.
Second, whenever you use RNG's, you should start it with set.seed().
Now, I have edited your code and it all works. No errors.
I got it rid of attach/detach and of the graphics.
And added spaces around some bits, such as <-, =, etc.
set.seed(187) # Make the results reproducible
m <- runif(1,0,1)
m
mres <- m*(seq(1, 12))
mres
ssd <- rexp(1, 1)
ssd
devs <- rep(0, length(mres))
for(i in 1:length(mres)){
devs[i] <- rnorm(1, 0, ssd)
}
devs
sales <- round((mres+devs)*1000)
ptr <- cbind(1:length(sales), sales, sales, sales)
ptr
sdf <- data.frame(1:nrow(ptr), sales)
colnames(sdf)<-c("monat", "mitte")
sdf
#attach(sdf)
s.lm <- lm(mitte ~ monat, sdf)
s.lm
news <- data.frame(monat = nrow(sdf) + 1)
news
fcs <- predict(s.lm, newdata = news, interval = "predict")
fcs
ptr <- rbind(ptr, c(1 + nrow(ptr), fcs[2], fcs[1], fcs[3]))
ptr
highdf <- data.frame(ptr[, c(1, 4)])
colnames(highdf) <- c("month","sales")
highdf
#detach(sdf)
#attach(highdf)
h.lm <- lm(sales ~ month, highdf)
h.lm
news <- data.frame(month = nrow(ptr) + 1)
news
hcs <- predict(h.lm, newdata = news, interval = "predict")
hcs
On 1/31/2018 5:20 PM, WRAY NICHOLAS via R-help wrote:
> I have realised that I should have used "detach" before attaching another
> dataframe, but even when I do this it's still giving me lots of lines, rather
> than just one:
>
> My code:
>
>
> m<-runif(1,0,1)
> m
> mres<-m*(seq(1,12))
> mres
> ssd<-rexp(1,1)
> ssd
> devs<-rep(0,length(mres))
> for(i in 1:length(mres)){devs[i]<-rnorm(1,0,ssd)}
> devs
> plot(-10,-10,xlim=c(1,24),ylim=c(0,20000))
> sales<-round((mres+devs)*1000)
>
> points(sales,pch=19)
>
> ptr<-cbind(1:length(sales),sales,sales,sales)
>
> ptr
> sdf<-data.frame(cbind(1:nrow(ptr),sales))
> sdf
>
> colnames(sdf)<-c(“monat”,“mitte”)
> sdf
> attach(sdf)
> s.lm<-lm(mitte~monat)
>
> s.lm
> abline(s.lm,lty=2)
> news<-data.frame(monat=nrow(sdf)+1)
> news
> fcs<-predict(s.lm,news,interval=“predict”)
> fcs
>
> points(1+nrow(ptr),fcs[,1],col=“grey”,pch=19)
> points(1+nrow(ptr),fcs[,2])
> points(1+nrow(ptr),fcs[,3])
> ptr<-rbind(ptr,c(1+nrow(ptr),fcs[2],fcs[1],fcs[3]))
> ptr
>
> highdf<-data.frame(ptr[,c(1,4)])
> highdf
> colnames(highdf)<-c(“month”,“sales”)
> highdf
> detach(sdf)
> attach(highdf)
> h.lm<-lm(highdf[,2]~highdf[,1])
> h.lm
> abline(h.lm,col="gray",lty=2)
> news<-data.frame(month=nrow(ptr)+1)
> news
> hcs<-predict(h.lm,news,interval=“predict”)
> hcs
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list