lm()在使用将预测变量设置为因子的poly()时中断

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

我正在尝试对分类预测变量和连续结果变量之间的关系进行建模。为此,我使用了lm()。由于它是分类变量,因此正确的做法是将其转换为因子变量类型。但是,将poly()用作预测变量的回归项时,如果将预测变量设置为一个因子,则会导致lm()中断。另一方面,如果我在不使用lm()的情况下运行poly()(但确实将预测变量保留为因子)保留poly()但未将预测变量转换为因子(让其为数字)-那么lm()不会中断。我不明白为什么它会破裂,而且我不明白在不破裂时我是否可以相信结果。

数据

有关50名篮球运动员的数据。一栏(PosCode)关于玩家在游戏中的位置,另一栏(Height)关于玩家的身高。

data <-
structure(list(Player = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 
44, 45, 46, 47, 48, 49, 50), PosCode = c(3, 3, 4, 1, 4, 1, 3, 
1, 2, 2, 4, 1, 5, 5, 2, 1, 2, 5, 4, 4, 5, 4, 4, 4, 2, 3, 2, 3, 
1, 1, 2, 4, 1, 2, 3, 1, 5, 4, 3, 4, 4, 1, 1, 4, 5, 1, 1, 1, 5, 
2), Height = c(176.1, 179.1, 183.1, 169.7, 177.3, 179, 176.4, 
174.9, 180.2, 176.5, 178.6, 167.9, 183.4, 166.2, 189.5, 171.9, 
188.5, 172.6, 167.7, 172.6, 186.9, 163.8, 179.3, 165.4, 182.2, 
166.1, 176.8, 171.9, 173.8, 163, 172.5, 184.9, 170.4, 170.6, 
166.8, 172.6, 184.3, 163.3, 182.4, 165.8, 173.4, 182.1, 172.9, 
184.9, 173.2, 185.8, 161.4, 186, 178.4, 170.7)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))


> data
## # A tibble: 50 x 3
##    Player PosCode Height
##    <dbl>   <dbl>  <dbl>
##  1      1       3   176.
##  2      2       3   179.
##  3      3       4   183.
##  4      4       1   170.
##  5      5       4   177.
##  6      6       1   179 
##  7      7       3   176.
##  8      8       1   175.
##  9      9       2   180.
## 10     10       2   176.
## # ... with 40 more rows

数据建模

我想知道我是否可以根据他们在游戏中的位置来预测他们的身高。由于位置是分类的(可能有5个位置),因此此变量应该是具有5个级别的因子类型。

library(tidyverse)
library(magrittr) 

data %<>% mutate_at(vars(PosCode), ~ as.factor(.)) ## convert PosCode from dbl to fct

使用lm()进行建模不使用 poly()

lm(Height ~ PosCode, data = data)

## Call:
## lm(formula = Height ~ PosCode, data = data)
## 
## Coefficients:
## (Intercept)     PosCode2     PosCode3     PosCode4     PosCode5  
##    173.6714       4.9397       0.4429       0.1824       4.1857  

使用lm()建模[[使用 poly()

lm(Height ~ poly(PosCode ,1), data = data) ## Error in qr.default(X) : NA/NaN/Inf in foreign function call (arg 1) ## In addition: Warning messages: ## 1: In mean.default(x) : argument is not numeric or logical: returning NA ## 2: In Ops.factor(x, xbar) : ‘-’ not meaningful for factors
如果预测变量不是一个因素,则与poly()无关,都没有问题

## convert PosCode from fct back to dbl data %<>% mutate_at(vars(PosCode), ~ as.double(.)) ## lm() without poly() lm(Height ~ PosCode, data = data) Call: lm(formula = Height ~ PosCode, data = data) ## Coefficients: ## (Intercept) PosCode ## 174.3848 0.3112 ## lm() with poly() lm(Height ~ poly(PosCode ,1), data = data) ## Call: ## lm(formula = Height ~ poly(PosCode, 1), data = data) ## Coefficients: ## (Intercept) poly(PosCode, 1) ## 175.256 3.173

但是很明显,将PosCode视为dbl而不是fct会以错误的方式更改模型。

底线

我不明白当将预测变量设置为因子变量时,为什么在poly()中包含lm()会破坏它。

r regression lm poly
1个回答
0
投票
来自help("poly")

x一个

数字向量,用于在其中求多项式。

因此,您不能使用poly()中的因子。这是可以预期的,因为必须将分类变量(即因子)重新编码为例如虚拟变量。例如,对于分类变量整体而言或对编码变量(虚拟变量)都没有二次效应是没有道理的。 (从实体的角度看这没有意义,但是从一个对统计无视的角度来看,对仅具有0和1的虚拟变量进行平方运算也没有多大意义。)

[您会看到lm()重新编码了因子,因为您在第一个模型中获得了四个变量PosCode的系数(比类别数少一个)。

最后,poly()没什么用,除非您将其参数degree设置为值> 1

© www.soinside.com 2019 - 2024. All rights reserved.