R误差下的Boxcox()变换

h5qlskok  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(199)
cellphone = read.csv("/Users/crystalchau/Desktop/UICT-CELL_IND.csv", nrows = 25, colClasses = c(NA,NA,"NULL")) 

cellphone = cellphone[nrow(cellphone):1,]

cellphone.ts = ts(cellphone, frequency = 1)

ts.plot(cellphone.ts, ylab = "Mobile Cellular Telephone Subscriptions")

title(expression(Mobile~Celluar~Telephone~Subscriptions))

par(mfrow=c(1,2))

cellphone = read.csv("/Users/crystalchau/Desktop/UICT-CELL_IND.csv", nrows = 25, colClasses = c("NULL",NA,"NULL"))

cellphone = cellphone[nrow(cellphone):1,]

cellphone.ts = ts(cellphone, frequency = 1)

acf(cellphone.ts, lag.max = 10)

pacf(cellphone.ts, lag.max = 10)

cellphone.ts = ts(cellphone, frequency = 12)

decompose_cellphone = decompose(cellphone.ts, type = "multiplicative")

plot(decompose_cellphone)

library(MASS)

bcTransform = boxcox(cellphone ~ as.numeric(1:length(cellphone)), lambda = seq(-1, 1, length = 10))

plot(bcTransform, type = 'l', axes = FALSE)

它不允许我运行boxcox转换行,并给出错误消息:
boxcox.default(手机.ts ~ as.数字(1:长度(手机.ts))中出错,:响应变量必须为正数
我哪里做错了?

sigwle7e

sigwle7e1#

该错误表明数据中存在零值或无穷大值(在本例中为cellphone)。
'* 在线性回归中,box-cox变换广泛用于变换目标变量,以便满足线性和正态性假设。但box-cox变换只能用于严格正的目标值。如果目标(因)变量中有负值,则不能使用box-cox和对数变换。*'(ref
可以通过向iris数据集添加负值来重现该误差。

library(MASS)

data(iris)

#no negatives, no error

boxcox(iris$Petal.Width ~ as.numeric(1:length(iris$Species)), lambda = seq(-1, 1, length = 10))

#add negatives

iris$Petal.Width2<-iris$Petal.Width-5

#gives error

boxcox(iris$Petal.Width2 ~ as.numeric(1:length(iris$Species)), lambda = seq(-1, 1, length = 10))

#Error in boxcox.default(iris$Petal.Width2 ~ as.numeric(1:length(iris$Species)),  : 
#response variable must be positive

您可以考虑尝试Yeo-Johnson转换。这类似于box-cox,但允许负值。(see here

相关问题