坚持数据(虹膜)Q.#6

时间:2017-12-04 01:52:19

标签: r statistics

我遇到了以下问题:

    # 6. Suppose from the MLR model, you want to predict the Sepal
    #    Length of a flower, where the Sepal Width, Petal Length and
    #    Petal Width are 4, 6, 2.3 respectively. What is the 90%
    #    confidence interval (CI) of the mean response? What is the
    #    90% prediction interval (PI) of a single response? What is
    #    the difference between CI and PI?
    #
    colnames(iris) = c("Sepal.Length", "Sepal.Width", "Petal.Length",
               "Petal.Width", "Species")
    Y = iris$Sepal.Length; X1 = iris$Sepal.Width
    X2 = iris$Petal.Length; X3 = iris$Petal.Width
    Par1 = 4
    Par2 = 6
    Par3 = 2.3
    y = 1.85600 + 0.65084 * Par1 + 0.70913 * Par2 + (-0.55648) * Par3
    y
    m3 = lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, 
    data = iris)
    summary(m3)
    CI2 = predict(y, int = "confidence", level=0.90) # for mean response
    CI2
    #prediction interval (ignore warning)
    PI = predict(y, int = "prediction", level=0.90) # for single response
    PI

我一直收到一条错误消息,         UseMethod中的错误(“预测”):           没有适用于“预测”的方法应用于类的对象         “c('double','numeric')”

我做错了什么?我需要这个问题的帮助才能回答下一个问题而且我被困住了。

1 个答案:

答案 0 :(得分:1)

# 6. Suppose from the MLR model, you want to predict the Sepal
#    Length of a flower, where the Sepal Width, Petal Length and
#    Petal Width are 4, 6, 2.3 respectively. What is the 90%
#    confidence interval (CI) of the mean response? What is the
#    90% prediction interval (PI) of a single response? What is
#    the difference between CI and PI?
#

# Get a clean version of the data
data(iris)
# Check to make sure the column names are as expected
colnames(iris)
# Fit the model
m3 <- lm(data=iris,Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width )
# see what is in the object
names(m3)
# Check the class of iris 
sapply(iris,class)
# create data used in the prediction
newdata <- data.frame(Sepal.Width = c(4), Petal.Length = c(6), Petal.Width = c(2.3))
# use the model to predict
CI2 <- predict(m3 ,newdata = newdata, int = "confidence", level=0.90) # for mean response
CI2