I have data X and Y, and I want to predict what the very next point would be based off the model. This is what I have:>model=lm(x~y)I think I want to use the predict function, but I'm not exactly sure what to do. Thank you! -- View this message in context: http://r.789695.n4.nabble.com/Linear-Model-Prediction-tp4637644.html Sent from the R help mailing list archive at Nabble.com.
On 24.07.2012 20:20, cm wrote:> I have data X and Y, and I want to predict what the very next point would be > based off the model. This is what I have: >> model=lm(x~y)Hmmm, are you sure about the above code?> I think I want to use the predict function, but I'm not exactly sure what to > do.Yes, use predict(). Is this homework? Uwe Ligges> > Thank you! > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Linear-Model-Prediction-tp4637644.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Hello, The example below gives you some information about using predict().?? This was originally used to predict NAs. dat1<-data.frame(Year=1962:1986,Discharge=c(rnorm(15,25),rep(NA,5),1:5)) lm1<-lm(Discharge~Year,dat1) dat2<-data.frame(Year=dat1[,1]) dat1$fit<-predict(lm1,newdata=dat2) #The code below is to replace the NA values with predicted. #dat1<-within(dat1,{Dischargenew<-ifelse(is.na(Discharge)==T,fit,Discharge)}) #dat1new<-dat1[,c(1:2,4)] A.K. ----- Original Message ----- From: cm <bunnylover23 at optonline.net> To: r-help at r-project.org Cc: Sent: Tuesday, July 24, 2012 2:20 PM Subject: [R] Linear Model Prediction I have data X and Y, and I want to predict what the very next point would be based off the model. This is what I have:>model=lm(x~y)I think I want to use the predict function, but I'm not exactly sure what to do. Thank you! -- View this message in context: http://r.789695.n4.nabble.com/Linear-Model-Prediction-tp4637644.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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.
On Tue, Jul 24, 2012 at 2:06 PM, <bunnylover23 at optonline.net> wrote:> Yes, why wouldn't I? It's a linear model between two sets of data: x and y.Conventionally, one predicts y based on x -- which is specified y ~ x, not x ~ y. (Predictors on the RHS, predicted on the LHS)> > Also, what would the new data be if i want to predict into the future? So, > for example, the data goes from a month ago to today. I want to predict what > tomorrow's data would be. So what is "newdata"?Well, it sounds like you really need a time series model, not a linear regression. Linear regressions are more or less unordered in x: that is, there's no unique "next" value for "x" -- there's just what happens next. For instance, if I were doing a gas law experiment and I saw the pressure of some fixed amount and volume of helium at temps 30, 40 25, 50, 75, and 20 C -- what would I expect from my next experiment? Who knows -- you haven't told me the independent variable yet. Time series models on the other hand have a well defined "next observation". It is sometimes possible (though not advisable) to fake a time series model by regressing on the date (after conversion to a number) but you'll still have to say what the next input is by converting the date of the future obs to a number again. This is probably not statistically advisable. Incidentally, please keep posts on the list archives and cc R-help unless there's good reason to keep the discussion private. Michael