Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R回归模型_R_Linear Regression - Fatal编程技术网

R回归模型

R回归模型,r,linear-regression,R,Linear Regression,我试图在R中进行回归分析,但是,我有定性的变量,例如(1=餐厅,2=电话)。我使用函数因子对它们进行了更改,但是,我需要分离这些值,以便有两个不同的变量用于回归分析。我如何才能做到这一点?假设您的数据位于数据帧中: dat <- data.frame(var1 = c(1,2,1,1,1,2), var2 = c("IceCream", "Batman", "IceCream", "Batman", "Batman")) 另一种方式: dat[dat$var1==1,] dat_sp

我试图在R中进行回归分析,但是,我有定性的变量,例如(1=餐厅,2=电话)。我使用函数因子对它们进行了更改,但是,我需要分离这些值,以便有两个不同的变量用于回归分析。我如何才能做到这一点?

假设您的数据位于数据帧中:

dat <- data.frame(var1 = c(1,2,1,1,1,2), var2 = c("IceCream", "Batman", "IceCream", "Batman",  "Batman"))
另一种方式:

dat[dat$var1==1,]
dat_split <- split(dat, dat$var1)
dat_split$`1`
另一种方式:

dat[dat$var1==1,]
dat_split <- split(dat, dat$var1)
dat_split$`1`

dat\u split您可以使用
model.matrix
。使用上面@thelatemail的示例:

y <- rnorm(100) 
x <- sample(factor(c("a","b","c")), 100, replace=TRUE)
head(x)
[1] a b a c a c
Levels: a b c

x1 <- model.matrix(~x-1)
head(x1)
  xa xb xc
1  0  0  1
2  1  0  0
3  0  0  1
4  0  0  1
5  0  1  0
6  0  1  0

y你没有。R是聪明的,当把它放进模型中时,它会比较你因子的每一个层次。例如:
y不确定这是如何回答这个问题的。据我所知,问题在于拟合一个包含分类预测变量的线性模型。这肯定是一个措辞糟糕的问题。我把它理解为试图在R中分离因子进行回归分析。