开发线性回归模型时出错/ R 没有响应

问题描述 投票:0回答:1

我正在使用 r-studio 分析与房地产销售相关的数据集,我试图做的是开发一个模型。一切都很顺利,但是这段代码

lm_model <- lm(`SALE PRICE` ~ ., data = trainData)
不管用。这意味着它没有响应也没有提供任何输出。

这是我的代码

# Check for non-numeric characters
non_numeric <- !grepl("^\\d+$", property_data$`SALE PRICE`)

# Display unique non-numeric values
unique(property_data$`SALE PRICE`[non_numeric])

# Remove non-numeric characters and convert to numeric
property_data$`SALE PRICE` <- as.numeric(gsub("[^0-9]", "", property_data$`SALE PRICE`))


# Load necessary libraries
library(caret)
library(glmnet)


# Create dummy variables for categorical features with unique column names
dummy_vars <- dummyVars(~ BOROUGH + `GROSS SQUARE FEET` + `BUILDING CLASS CATEGORY` + `TAX CLASS AT TIME OF SALE`, data = property_data, fullRank = TRUE)
dummy_data <- data.frame(predict(dummy_vars, newdata = property_data)) 

#Prefix the dummy variable column names with a unique identifier
colnames(dummy_data) <- paste("DUMMY_", colnames(dummy_data), sep = "")

# Combine the dummy variables with the original data
property_data <- cbind(property_data, dummy_data)

# Impute missing values in SALE_PRICE with the mean
mean_sale_price <- mean(property_data$`SALE PRICE`, na.rm = TRUE)
property_data$`SALE PRICE`[is.na(property_data$`SALE PRICE`)] <- mean_sale_price


# Split the dataset into training (70%) and testing (30%) sets
set.seed(123)
trainIndex <- createDataPartition(property_data$`SALE PRICE`, p = 0.7, list = FALSE)
trainData <- property_data[trainIndex, ]
testData <- property_data[-trainIndex, ]

到目前为止,它有效。

# Build the regression models
# Simple Linear Regression
lm_model <- lm(`SALE PRICE` ~ ., data = trainData)

这位没有给出任何回应。代码仍然运行没有任何反应。

我想开发一个回归模型来查找销售价格。将使用线性回归和套索回归

r linear-regression lasso-regression
1个回答
0
投票

tl;dr 我认为这非常慢。特别是,线性回归的计算复杂度与列数的平方成正比。以下是接近您大小的问题的一些基准(我针对不同的行数运行了它们,但您可以尝试对不同的列数执行相同的操作...)

在我的机器上,对于整个问题的大小 (59K x 5.8K) 的线性回归,

lm.fit
需要 9 分钟,而
fastLm
实际上更慢,为 16 分钟。

如果您对一些较小的问题进行基准测试,您至少能够预测应该等待完整问题的时间(即,是否需要 5 或 10 分钟,或者 5 或 10 天......)

可能通过将整个问题设置为稀疏矩阵问题来获得一些速度,但这并不是微不足道的AFAICT ...

在这张图片(下面的代码)中,虚线代表

time = a*(# rows)^(1.5)
形式的曲线。


nr <- 59185
nc <- 5759
m <- as.data.frame(y = rnorm(nr),
                   matrix(rnorm(nr*nc), ncol = nc))
f <- function(x) format(object.size(x), units = "Gb")
f(m)  ## 2.5 Gb

X <- as.matrix(m[,-1])
y <- m[,1]

library(RcppEigen) ## for fastLm
library(tidyverse)
theme_set(theme_bw())

rvec <- round(10^(seq(3, log10(nr), length.out = 10)))
nr <- length(rvec)
tmat.lm <- tmat.flm <- matrix(NA, nrow=nr, ncol=3)
for (i in seq_along(rvec)) {
    w <- seq(rvec[i])
    cat(rvec[i],"\n")
    tmat.lm[i,] <- system.time(lm.fit(X[w,], y[w]))[1:3]
    tmat.flm[i,] <- system.time(fastLm(X[w,], y[w]))[1:3]
}

tmat.lm <- tmat.lm[1:nr,]
tmat.flm <- tmat.flm[1:nr,]

f2 <- function(x, lab) {
    colnames(x) <- c("user", "self", "elapsed")
    data.frame(method = lab, size = rvec, x)
}

rr <- (bind_rows(f2(tmat.lm, "lm"), f2(tmat.flm, "flm"))
    |> pivot_longer(-c(method, size), values_to = "time")
    |> filter(name == "elapsed")
)

gg0 <- ggplot(rr, aes(size, time, colour = method)) +
    scale_x_log10() +
    scale_y_log10() +
    geom_point() +
    geom_smooth() +
    labs(x = "size (# rows)", y = "time (seconds)")

gg0 +  geom_function(
           colour = "black",
           aes(y = NA),
           data = data.frame(size = unique(rr$size)),
           fun = function(x) 3*(x/1000)^(1.5),
           lty = 2)

ggsave("lm_bench.png")
© www.soinside.com 2019 - 2024. All rights reserved.