2

Rで観測された散布図と予測された散布図を描くにはどうすればよいですか? また、回帰直線も表示したいと思います。

library(car)
summary(Prestige)
head(Prestige)
testidx <- which(1:nrow(Prestige)%%4==0)
prestige_train <- Prestige[-testidx,]
prestige_test <- Prestige[testidx,]

model <- glm(prestige~., data=prestige_train)
# Use the model to predict the output of test data 
prediction <- predict(model, newdata=prestige_test)
# Check for the correlation with actual result
plot(prediction, prestige_test$prestige)

編集:

abline(model)

上記の機能ablineは動作していないようです。線を引いていますが、間違っているようです。

ありがとう

4

1 に答える 1

2

ハドリー・ウィッカムによるもっと美しい (そしておそらくもっと情報に基づいた) 説明が存在する場合、私が答え全体を書き出す必要はあまりないと思います。ここ(最初の部分) とここ(2 番目のより高度な部分) にあります。

編集:最初からより具体的な答えを出さなかったことをお詫びします。プロットの作成方法は次のとおりです。

# define training sample
sample <- sample(nrow(Prestige),40,replace=FALSE)

training.set <- Prestige[sample,]
test.set <- Prestige[-sample,]
model <- glm(prestige ~ .,data=training.set)
# getting predicted values
test.set$predicted <- predict(model,newdata=test.set)

# plotting with base plot 
with(test.set,plot(prestige,predicted))
# plotting with ggplot2
require(ggplot2)
qplot(prestige,predicted,data=test.set)

これがあなたの質問に答えることを願っています。

PS質問は確かにSOの方が適切です。数分で回答されたはずです:)

于 2013-04-25T20:27:57.093 に答える