简体   繁体   English

R-如何根据人工数据预测值

[英]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. 为了澄清起见,我想使用大小为100的测试数据从大小为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. 提前致谢。

The variable in your linear regression model is called x.train . 线性回归模型中的变量称为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. 但是,在经过testdata ,变量名是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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM