R错误中的Boxcox()转换

时间:2018-05-29 03:50:12

标签: r error-handling time-series transformation

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)),:响应变量必须是   阳性

我做错了什么?

1 个答案:

答案 0 :(得分: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,但允许否定。 (see here