Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ';预测';与手动使用';总结';_R_Model_Floating Accuracy_Predict - Fatal编程技术网

R ';预测';与手动使用';总结';

R ';预测';与手动使用';总结';,r,model,floating-accuracy,predict,R,Model,Floating Accuracy,Predict,让我举个例子来说明我的困惑 #making datasets x1<-iris[,1] x2<-iris[,2] x3<-iris[,3] x4<-iris[,4] dat<-data.frame(x1,x2,x3) dat2<-dat[1:120,] dat3<-dat[121:150,] #Using a linear model to fit x4 using x1, x2 and x3 where training set is first 1

让我举个例子来说明我的困惑

#making datasets
x1<-iris[,1]
x2<-iris[,2]
x3<-iris[,3]
x4<-iris[,4]
dat<-data.frame(x1,x2,x3)
dat2<-dat[1:120,]
dat3<-dat[121:150,]

#Using a linear model to fit x4 using x1, x2 and x3 where training set is first 120 obs.
model<-lm(x4[1:120]~x1[1:120]+x2[1:120]+x3[1:120])

#Usig the coefficients' value from summary(model), prediction is done for next 30 obs.
-.17947-.18538*x1[121:150]+.18243*x2[121:150]+.49998*x3[121:150]

#Same prediction is done using the function "predict"
predict(model,dat3)
#制作数据集

x1差异非常小,我认为这是由于您使用的系数的准确性(例如,截距的实际值是
-0.17947075338464965610…
而不仅仅是
-.17947

事实上,如果取系数值并应用公式,结果等于预测:

intercept <- model$coefficients[1]
x1Coeff <- model$coefficients[2]
x2Coeff <- model$coefficients[3]
x3Coeff <- model$coefficients[4]

intercept + x1Coeff*x1[121:150] + x2Coeff*x2[121:150] + x3Coeff*x3[121:150]

截距差异非常小,我认为这只是由于您使用的系数的准确性(例如,截距的实际值是
-0.17947075338464965610…
而不仅仅是
-.17947

事实上,如果取系数值并应用公式,结果等于预测:

intercept <- model$coefficients[1]
x1Coeff <- model$coefficients[2]
x2Coeff <- model$coefficients[3]
x3Coeff <- model$coefficients[4]

intercept + x1Coeff*x1[121:150] + x2Coeff*x2[121:150] + x3Coeff*x3[121:150]

intercept您可以稍微清理一下代码。要创建培训和测试数据集,可以使用以下代码:

# create training and test datasets
train.df <- iris[1:120, 1:4] 
test.df <- iris[-(1:120), 1:4]

# fit a linear model to predict Petal.Width using all predictors
fit <- lm(Petal.Width ~ ., data = train.df)
summary(fit)

# predict Petal.Width in test test using the linear model
predictions <- predict(fit, test.df)

# create a function mse() to calculate the Mean Squared Error
mse <- function(predictions, obs) {
  sum((obs - predictions) ^ 2) / length(predictions)
}

# measure the quality of fit
mse(predictions, test.df$Petal.Width)
#创建培训和测试数据集

train.df您可以稍微清理一下代码。要创建培训和测试数据集,可以使用以下代码:

# create training and test datasets
train.df <- iris[1:120, 1:4] 
test.df <- iris[-(1:120), 1:4]

# fit a linear model to predict Petal.Width using all predictors
fit <- lm(Petal.Width ~ ., data = train.df)
summary(fit)

# predict Petal.Width in test test using the linear model
predictions <- predict(fit, test.df)

# create a function mse() to calculate the Mean Squared Error
mse <- function(predictions, obs) {
  sum((obs - predictions) ^ 2) / length(predictions)
}

# measure the quality of fit
mse(predictions, test.df$Petal.Width)
#创建培训和测试数据集

train.df这是完全正常的第一个系数不是
-.17947
(它只是前4位小数,并且
打印(coef(model)[1],数字=12)
给出
-0.179470753385
)尝试使用coef代替
c(crossprod(coef(model),rbind(1,x1[121:150],x2[121:150],x3[121:150]))
这是完全正常的,第一个系数不是
-.17947
(它只是前4位小数,并且
打印(coef(model)[1],数字=12)
给出
-0.179470753385
)试着用coef代替
c(crossprod(coef(model),rbind(1,x1[121:150],x2[121:150],x3[121:150])