iT邦幫忙

0

非線性迴歸-多項式迴歸 (polynomial regression in r)

廢話不多說,直接附上code
影片含有程式碼詳細解說,若有誤再煩請告知,謝謝/images/emoticon/emoticon25.gif

Yes

data(iris)
iris <- iris[,-c(5)]
#找自變數
library(rminer) 
model <- lm(Sepal.Length~Sepal.Width+Petal.Length+Petal.Width,iris)
lm <- Importance(model,iris,measure="AAD")
lm$imp
#檢查離群值
par(mfrow=c(1,3))
boxplot(iris$Sepal.Width)$out
boxplot(iris$Petal.Length)$out
boxplot(iris$Petal.Width)$out
par(mfrow=c(1,3))
scatter.smooth(x=iris$Sepal.Width, y=iris$Sepal.Length)
scatter.smooth(x=iris$Petal.Length, y=iris$Sepal.Length)
scatter.smooth(x=iris$Petal.Width, y=iris$Sepal.Length)
#隨機抽樣
n <- nrow(iris)
set.seed(1117)
subiris <- sample(seq_len(n), size = round(0.7 * n))
traindata <- iris[subiris,]
testdata <- iris[ - subiris,]

#建模
model <- lm(Sepal.Length~poly(Sepal.Width,3)+Petal.Length+Petal.Width,traindata)
#條件2~4
library(car)
ncvTest(model)#>a 殘差變異數有同質性
shapiro.test(model$residuals) #>a 殘差常態
library(lmtest)
dwtest(model)#>a 殘差獨立
vif(model)  #<10 ok 10~100可能過度配適

#預測
future <- predict(model,testdata)
future <- as.data.frame(future)
final <- cbind(future,testdata)
library(dplyr)
final <- mutate(final,mape=abs(future-Sepal.Length)/Sepal.Length)
mean(final$mape)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言