当我尝试使用olsrr
之类的ols_step_both_p()
中的函数时,出现如下所示的未找到对象错误:
eval(model $ call $ data)中的错误:找不到对象'mtcars1'
我想也许我正在使用的数据框搞砸了,因为有一列包含日期,而另一列包含整数。因此,我尝试在“ mtcars”上使用这些功能。
有效!很棒。...但是当我对mtcars进行细微更改时....就像创建一个名为mtcars1的数据框副本一样,我又得到了错误。
test_step_regress <- function()
{
mtcars1 <- mtcars
model <- lm(mpg ~ ., data = mtcars1)
k1 <- ols_step_both_p(model)
print(k1)
}
我得到的错误:
Error in eval(model$call$data) : object 'mtcars1' not found 5. eval(model$call$data) 4. eval(model$call$data) 3. ols_step_both_p.default(model) 2. ols_step_both_p(model) at regress_step.r#5 1. test_step_regress()
我的会话信息():
sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 18.3
Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
答案 0 :(得分:0)
在call
中重新分配model
属性将解决此问题
test_step_regress <- function(){
mtcars1 <- mtcars
model <- lm(mpg ~ ., data = mtcars1)
print("model call data after creating the model")
print(model$call$data)
model$call$data <- mtcars1
print("model call data after resassigning")
print(model$call$data)
k1 <- ols_step_both_p(model)
print(k1)
}
test_step_regress()
#[1] "model call data after creating the model"
#mtcars1
#[1] "model call data after resassigning"
# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#...
#..
#Stepwise Selection Method
#---------------------------
#Candidate Terms:
#1. cyl
#2. disp
#3. hp
#4. drat
#5. wt
#6. qsec
#7. vs
#8. am
#9. gear
#10. carb
#We are selecting variables based on p value...
#Variables Entered/Removed:
#✔ wt
#✔ cyl
#No more variables to be added/removed.
#Final Model Output
#------------------
# Model Summary
#--------------------------------------------------------------
#R 0.911 RMSE 2.568
#R-Squared 0.830 Coef. Var 12.780
#Adj. R-Squared 0.819 MSE 6.592
#Pred R-Squared 0.790 MAE 1.921
#--------------------------------------------------------------
# RMSE: Root Mean Square Error
# MSE: Mean Square Error
# MAE: Mean Absolute Error
# ANOVA
#--------------------------------------------------------------------
# Sum of
# Squares DF Mean Square F Sig.
#--------------------------------------------------------------------
#Regression 934.875 2 467.438 70.908 0.0000
#Residual 191.172 29 6.592
#Total 1126.047 31
#--------------------------------------------------------------------
Parameter Estimates
#----------------------------------------------------------------------------------------
# model Beta Std. Error Std. Beta t Sig lower upper
#----------------------------------------------------------------------------------------
#(Intercept) 39.686 1.715 23.141 0.000 36.179 43.194
# wt -3.191 0.757 -0.518 -4.216 0.000 -4.739 -1.643
# cyl -1.508 0.415 -0.447 -3.636 0.001 -2.356 -0.660
#----------------------------------------------------------------------------------------
Stepwise Selection Summary
#-------------------------------------------------------------------------------------
Added/ Adj.
#Step Variable Removed R-Square R-Square C(p) AIC RMSE
#-------------------------------------------------------------------------------------
# 1 wt addition 0.753 0.745 11.6270 166.0294 3.0459
# 2 cyl addition 0.830 0.819 1.2190 156.0101 2.5675
#-------------------------------------------------------------------------------------