- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
nrow(subs-6ren">
我正在尝试制作和测试一个线性模型,如下所示:
lm_model <- lm(Purchase ~., data = train)
lm_prediction <- predict(lm_model, test)
Product_Category_1
列的值存在于
test
中数据框但不是
train
数据框):
factor Product_Category_1 has new levels 7, 9, 14, 16, 17, 18
> nrow(subset(train, Product_Category_1 == "7"))
[1] 2923
> nrow(subset(test, Product_Category_1 == "7"))
[1] 745
> nrow(subset(train, Product_Category_1 == "9"))
[1] 312
> nrow(subset(test, Product_Category_1 == "9"))
[1] 92
train
的表格和
test
表明它们具有相同的因素:
> table(train$Product_Category_1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
110820 18818 15820 9265 118955 16159 2923 89511 312 4030 19113 3108 4407 1201 4991 7730 467 2430
> table(test$Product_Category_1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
27533 4681 4029 2301 29637 4005 745 22621 92 1002 4847 767 1033 299 1212 1967 100 645
>
最佳答案
目录:
predict
工作? train <- data.frame(y = runif(4), x = c(runif(3), NA), f = factor(letters[1:4]))
test <- data.frame(y = runif(4), x = runif(4), f = factor(letters[1:4]))
fit <- lm(y ~ x + f, data = train)
predict(fit, newdata = test)
#Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
# factor f has new levels d
lm
和
predict
工作。
table(train$f)
和
table(test$f)
它没有用,因为问题不是由变量
f
引起的但来自
NA
在
x
.
lm
和
glm
删除不完整的案例,即至少有一个
NA
的行(参见 ?
complete.cases
)进行模型拟合。他们必须这样做,否则用于 QR 分解的底层 FORTRAN 例程将失败,因为它无法处理
NA
.如果您查看
?lm
处的文档你会看到这个函数有一个参数
na.action
默认为
na.omit
.您也可以将其设置为
na.exclude
但是
na.pass
其中保留
NA
会导致 FORTRAN 错误:
fit <- lm(y ~ x + f, data = train, na.action = na.pass)
#Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
# NA/NaN/Inf in 'x'
NA
来自训练数据集。
train <- na.omit(train)
train$f
#[1] a b c
#Levels: a b c d
f
现在有一个未使用的级别
"d"
.
lm
和
glm
将在构建模型框架(以及后来的模型矩阵)时删除未使用的级别:
## source code of lm; don't run
mf$drop.unused.levels <- TRUE
mf[[1L]] <- quote(stats::model.frame)
mf <- eval(mf, parent.frame())
mf <- model.frame(y ~ x + f, data = train, drop.unused.levels = FALSE)
model.matrix(y ~ x + f, data = mf)
# (Intercept) x fb fc fd
#1 1 0.90021178 0 0 0
#2 1 0.10188534 1 0 0
#3 1 0.05881954 0 1 0
#attr(,"assign")
#[1] 0 1 2 2 2
#attr(,"contrasts")
#attr(,"contrasts")$f
#[1] "contr.treatment"
NA
虚拟变量系数
fd
.来自
drop.unused.levels = TRUE
由
lm
强制和
glm
:
mf <- model.frame(y ~ x + f, data = train, drop.unused.levels = TRUE)
model.matrix(y ~ x + f, data = mf)
# (Intercept) x fb fc
#1 1 0.90021178 0 0
#2 1 0.10188534 1 0
#3 1 0.05881954 0 1
#attr(,"assign")
#[1] 0 1 2 2
#attr(,"contrasts")
#attr(,"contrasts")$f
#[1] "contr.treatment"
fd
消失了,并且
mf$f
#[1] a b c
#Levels: a b c
"d"
级别将导致
predict
中的“新因子级别”错误.
lm
和
glm
在幕后做,使他们的调试生活更容易。
subset
争论。但是有一个潜在的陷阱:并非所有因子水平都可能出现在子集数据集中,因此在使用
predict
时您可能会得到“新的因子水平”。之后。
lm
的函数时,上述建议尤为重要。或
glm
.您希望您的功能健壮。要求您的函数返回信息性错误而不是等待
lm
和
glm
提示。
lm
和
glm
返回
xlevels
拟合对象中的值。它包含因子水平
实际上用于模型拟合。
fit$xlevels
#$f
#[1] "a" "b" "c"
xlevels
应该首先检查。
table
的东西计算每个因子水平有多少个案例,这里有一个方法:
Get number of data in each factor level (as well as interaction) from a fitted lm or glm [R] ,虽然制作模型矩阵可以使用很多 RAM。
predict
工作?
train
和
test
数据集(请参阅下一节),您需要在
test
中设置这些因子水平但不在
xlevels
至
NA
.然后
predict
只会预测
NA
对于这种不完整的情况。
train
和
test
因为他们想做交叉验证。第一步是申请
na.omit
在你的完整数据集上去掉
NA
噪音。然后我们可以对剩下的东西做一个随机分区,但是这种天真的方法可能会导致
test
中的一些因子水平但不在 train
(哎呀,我们在使用 predict
时得到“新因子水平”错误); train
中的一些因子变量删除未使用的级别后只有 1 个级别(哎呀,我们在使用 lm
和 glm
时出现“对比”错误); train
的模型矩阵是秩亏的(哎呀,我们在使用 predict
时收到“对秩亏模型的预测可能具有误导性”警告)。 lm
, Could anyone give me an example of the misleading case on “prediction from a rank-deficient”?但是,此类问题更难避免,尤其是当您有许多因素并且可能存在交互作用时。
关于r - 如何调试线性模型和预测的 "factor has new levels"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51552203/
我是业余时间的 Java 开发者,我只是想知道 level.tick() 和 Level.tick(level) ('Level' 是类的名称,'level' 是该类的对象) 我知道,由于 java
在 asp.net MVC 中的 Level Views Level 或 Model level 中验证更明智 以及有关 MVC 中验证的良好教程的链接? 最佳答案 验证应始终在可能的情况下在客户端和
当使用随机数生成器时,为了使新值具有更大的随机性,这是更好的使用方式: 有没有一种每次都实例化一个新的 RNG 实例然后返回一个值的方法? 在类级别有一个 RNG 实例,它在构造函数中实例化一次,并且
我在理解“单一责任原则”时遇到问题。应该在类级别或方法级别应用SRP。 可以说我有学生类,我需要创建学生,更新学生和删除学生。 如果我创建一个具有用于这三个操作的方法的服务类,则会违反SRP原则。 最
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
这个问题与语言无关。枚举级别之间有什么区别。在应用程序中,我看到许多 INFO 消息对我来说似乎是 DEBUG 信息,反之亦然。 最佳答案 对此有多种不同的解释。我个人的看法是: 信息 - 与申请状态
似乎最好只为顶级函数创建一次 FunPtr,而不是在需要时创建一个新的函数(同一函数)并处理其释放。 除了 foreign import ccall "wrapper" 之外,我是否忽略了获取 Fun
我正在经历以下 link了解在依赖倒置原则的背景下高级和低级模块的含义。 根据那里给出的解释,以下代码片段是一个好的/合适的示例吗? public class HighLevel { priv
我很好奇在 Java 应用程序中执行 URL 重写的最佳方法是什么。假设您有一个名为 test_app 的应用程序,我将其部署在 tomcat 上。 通过 URL 重写,我的意思是我不想通过 host
我试图找到最有效的方法来检查给定的字符串是否为回文。 首先,我尝试了蛮力,其运行时间为 O(N)。然后我通过只进行 n/2 次比较而不是 n 次来稍微优化代码。 代码如下: def palindrom
我有一个包含一些随机自然数的表格,例如: number ------- 2 3 1 我想从这个表中检索一个集合,其中每一行都将包含与值显示的次数一样多的次数,例如上面它将是: number -----
我一直在了解测试驱动开发的优势,并尝试使用 pytest 开发我的第一个 TDD 应用程序和 setuptools 开发 选项。到目前为止进展顺利。我有一个问题:我的 test_* 模块中应该将要测试
我想将特定记录器名称、特定级别或更高级别(比如 INFO 及以上)的消息记录到特定的日志处理程序,比如文件处理程序,同时仍然获取所有日志消息到控制台。 Python 是 2.7 版。 到目前为止我尝试
DOM Level 0 事件与 DOM Level 2 事件有什么区别?我问是因为有人告诉我 Firefox 和 IE 以不同的顺序调用它们,而我以前从未听过这些术语。 最佳答案 DOM Level
其实我对token不是很了解..当我阅读 googleresearch/bert model 时,我看到了这些词。 # In the demo, we are doing a simple class
假设我们在站点中有一个页面,它显示了数据库中的一些记录。 我们需要显示按某列排序的记录。 哪种方法可以提供更好的性能:检索从数据库中排序的数据或在网格上应用排序? 谢谢 最佳答案 这取决于您要显示的数
其实我对token不是很了解..当我阅读 googleresearch/bert model 时,我看到了这些词。 # In the demo, we are doing a simple class
我有以下 CSS 下拉菜单标记: FieldOne LevelOne FieldTwo LevelOne FieldThree LevelOne
这个问题已经有答案了: What is the difference between DOM Level 0 events vs DOM Level 2 events? (2 个回答) 已关闭 9 年
如何通过搜索数组(第 2 级)中的重复值来删除一行数组(第 1 级)? $array = array( 0 => array('name' => 'haha1', 'dateline' => '
我是一名优秀的程序员,十分优秀!