R 中的 Boxcox() 转换错误

问题描述 投票:0回答:1
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(cellphone.ts ~ 中的错误 as.numeric(1:length(cellphone.ts)), :响应变量必须是 积极

我做错了什么?

r error-handling time-series transformation
1个回答
0
投票

该错误表明数据中存在零或无限值(在本例中为

cellphone
)。

'在线性回归中,box-cox变换被广泛用于对目标变量进行变换,以满足线性和正态性假设。但 box-cox 变换只能用于严格为正的目标值。如果目标(因)变量中有负值,则无法使用 box-cox 和 log 变换。' (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
,但允许负数。 (看这里

© www.soinside.com 2019 - 2024. All rights reserved.