Javierdds
Javierdds

Reputation: 199

R - How to predict values from an artificial data

I'm getting a bit confused on how can I predict values from artificial data, so here's my problem.

I'm trying to do simple linear regression (predict) with the following data:

set.seed(1)
x.train<-runif(1000,0,2)
eps.train<-rnorm(1000,sd=0.1)
y.train<-sin(x.train)+eps.train
model<-lm(y.train~x.train)
confint(modelo,level=0.95)

So now, I think I must do something like:

set.seed(16)
x.test<-data.frame(runif(100,0,2))
eps.test<-rnorm(100,sd=0.1)
y.test<-sin(x.test)+eps.test
linear_prediction<-predict(model, x.test, interval="prediction")

For clarify things, I want to predict with test data of size 100 from the "original" data of size 1000.

I know I'm doing something wrong in the second part of my code, but I can't solve it myself. I'll appreciate all the help. Thanks in advance.

Upvotes: 0

Views: 47

Answers (1)

Jason Mathews
Jason Mathews

Reputation: 805

The variable in your linear regression model is called x.train. For example, printing your model gives,

model

Call:
lm(formula = y.train ~ x.train)

Coefficients:
(Intercept)      x.train  
     0.2246       0.4809  

But, while passing the testdata, the variable name is runif.100..0..2.. To avoid the warning message just change the variable name in your test data and rerun the predictions.

colnames(x.test) = c("x.train") 
linear_prediction<-predict(model, x.test, interval="prediction")

Upvotes: 2

Related Questions