gpt4 book ai didi

r - R错误中的Box Cox()转换

转载 作者:行者123 更新时间:2023-12-03 07:57:51 29 4
gpt4 key购买 nike

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转换行,并给我错误消息:

Error in boxcox.default(cellphone.ts ~ as.numeric(1:length(cellphone.ts)), : response variable must be positive



我究竟做错了什么?

最佳答案

该错误表明您的数据中存在零个或无限个值(在这种情况下为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)

关于r - R错误中的Box Cox()转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50576059/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com