我在 R 课上遇到了困难,需要帮助来修复下面脚本末尾的粗体部分。运行粗体代码后不断出现的错误是: xy.coords(x, y, xlabel, ylabel, log) 中的错误:“x”和“y”长度不同。为了添加一些上下文,我尝试根据我的数据集创建一个线性回归模型,然后在我的线性回归图上生成一条多项式回归线。先谢谢您的帮助!!!这是我的脚本:
library(rstudioapi)[text]
options(scipen = 999)
options(digits = 3)
rm(list=ls())
load("carData.rdata")
set.seed(25)
trainIndex <- sample(1:nrow(carData), trunc(nrow(carData) * 0.75))
train <- carData[trainIndex,]
test <- carData[-trainIndex,]
lm.results <- lm(Price~., data=train)
library(car)
numeric_columns <- sapply(carData, is.numeric)
numeric_data <- carData[, numeric_columns]
correlation_matrix <- cor(numeric_data)
vif_values <- vif(lm(Price ~ ., data = numeric_data))
print(correlation_matrix)
print(vif_values)
summary(lm.results)
train$Model <- factor(train$Model)
test$Model <- factor(test$Model)
test$Model <- factor(test$Model, levels = levels(train$Model))
predprice <- predict(lm.results, newdata = test)
rmse <- sqrt(mean((test$Price-predprice)^2, na.rm=TRUE))
print(round(rmse,2))
poly_results <- lm(Price ~ poly(predict(lm.results), 2), data = train)
poly_predprice <- predict(poly_results)
par(mar = c(3, 3, 1, 1))
#problematic line:
plot(train$Price,
predprice,
xlab = "Observed Price", ylab = "Predicted Price",
main = "Linear Regression: Observed vs. Predicted Prices",
cex.axis = 0.8)
lines(sort(train$Price), sort(poly_predprice), col = "blue")
您已将数据集拆分为 train 和 test,因此您现在正在处理两个不同的集合(具有不同数量的记录)。为了使
plot()
满意,x 和 y 序列的长度必须相等。例如,尝试使用长度为 3 和 2 ( plot(c(1,2,3), c(1,2))
) 的向量进行绘图会重现您所看到的错误。
为了获得有意义的观察和预测散点图,这些散点图也应该来自同一组(即训练)
以下示例使用
mtcars
数据集来实现可重复性:
# mtcars dataset, check `?mtcars` for more details
head(mtcars)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
str(mtcars)
#> 'data.frame': 32 obs. of 11 variables:
#> $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
#> $ disp: num 160 160 108 258 360 ...
#> $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
#> $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
#> $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
#> $ qsec: num 16.5 17 18.6 19.4 17 ...
#> $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
#> $ am : num 1 1 1 0 0 0 0 0 0 0 ...
#> $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
#> $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
set.seed(25)
# split dataset into train and test
trainIndex <- sample(1:nrow(mtcars), trunc(nrow(mtcars) * 0.75))
train <- mtcars[trainIndex,]
test <- mtcars[-trainIndex,]
# unmber records in train and test sets:
nrow(train)
#> [1] 24
nrow(test)
#> [1] 8
lm.results <- lm(mpg~., data=train)
# number of results when predicting on training set
length(predict(lm.results))
#> [1] 24
# number of results when predicting on test set
length(predict(lm.results, newdata = test))
#> [1] 8
# plot predictions vs observations
plot(predict(lm.results) ~ train$mpg)