R语言 如何在回归中包含因子

R语言 如何在回归中包含因子

分类变量(也被称为因子或定性变量)是将观察值分类的变量。它们是字符串或数字,在统计建模中被称为因子变量。将正常的字符串变量保存为因子可以节省大量的内存。因子也可以作为水平变量或标签变量来存储。它们有数量有限的不同值,称为水平。例如,个人的性别是一个分类变量,可以有两个级别: 男性或女性。 回归需要数字变量。因此,当研究人员想在回归模型中包括一个分类变量时,需要采取一些步骤来使结果可以解释。让我们通过一个R语言的代码例子来看看这一切。

在R语言中的实现

将字符串或数字存储为因子

首先,让我们创建一个样本数据集。

# creating sample
samp <- sample(0:1, 20, replace = TRUE)
samp
Bash

输出

[1] 1 1 0 1 1 1 0 0 1 0 0 1 1 1 1 1 1 1 0 1
Bash

转换设置为因子的数字。

samp <- sample(0:1, 20, replace = TRUE)
  
# converting sampleto factors
samp1 <- factor(samp)
  
# to find if its a factor lets use is.factor()
is.factor(samp1)
Bash

输出

[5]TRUE
Bash

现在对弦乐做同样的事情。

# creating string sample
str1 <- c("small", "big", "medium", "small", "small",
          "big", "medium", "medium", "big")
str1
  
# will show output of string
f.str1<-factor(str1)
  
# check if f.str1 is factor or not
is.factor(f.str1)
  
# check if str1 is factor or not
is.factor(str1)
Bash

输出

[1]"small"  "big"    "medium" "small"  "small"  "big"    "medium" "medium" "big"   
[10]TRUE
[12]FALSE


Bash

有标签的因素

# creating sample with labels
lab <- factor(samp1, labels = c("sweet", "bitter"))
lab
Bash

输出

bitter bitter sweet  bitter bitter bitter sweet  sweet  bitter sweet 
[11] sweet  bitter bitter bitter bitter bitter bitter bitter sweet  bitter
Levels: sweet bitter
Bash

有序因素

str1 <- c("small", "big", "medium", "small", "small",
          "big", "medium", "medium", "big")
  
# ordering the factors w.r.t levels
order <- ordered(str1, 
                 levels = c("small", "medium", "big"))
order
f.order <- factor(order)
Bash

输出

[1] small  big    medium small  small  big    medium medium big   
Levels: small < medium < big
Bash

另一种使一个因素有序化的方法是。

# another way to order 
f.order = factor(str1, 
                 levels = c("small", "medium", "big"),
                 ordered = TRUE)
Bash

为了找到平均数

mean(samp1)
  
# shows NA has output
mean(as.numeric(levels(samp1)[samp1]))
Bash
NA
0.7
Bash

撤消级别

f.new <- f.order[f.order != "small"]
f.new
Bash

输出

[1] big    medium big    medium medium big   
Levels: small < medium < big
Bash

用回归法实施

将实验视为学生在节日期间呆在学校的时间。

# consider a dataframe student
student <- data.frame(
    # id of students
    id = c (1:5), 
    
    # name of students  
    name = c("Payal", "Dan", "Misty", "Ryan", "Gargi"),
    
    # gender of students
    gender = c("F", "M", "F", "M", "F"),
    
    # gender represented in numbers F-1,M-0
    gender_num = c(1, 0, 1, 0, 1),
    
    # the hours students stay at fests
    hours = c(2.5, 4, 5.3, 3, 2.2)
)
student
Bash

输出

  id  name   gender gender_num hours
1  1 Payal      F          1   2.5
2  2   Dan      M          0   4.0
3  3 Misty      F          1   5.3
4  4  Ryan      M          0   3.0
5  5 Gargi      F          1   2.2
Bash

回归方程为

**y = b 0 + b1 *x **

其中

y: 在预测变量(x)的基础上预测的输出变量。

**b 0 + b1 : **β系数,分别代表截距和斜率。

**b 0 + b1: **如果一个学生是男性, **b 0: **如果一个学生是女性。这些系数可以解释如下。

  • **b 0 **是女学生在节庆活动中停留的平均时间。
  • **b 0 + b1 **是男学生在节庆活动中停留的平均小时数,而
  • **b 1 **是男女学生之间的平均时间差。

R通过以下代码自动创建虚拟变量。

# making the regression model
model <- lm(hours ~ gender, data = student) 
summary(model)$coef
Bash

输出

            Estimate Std. Error   t value   Pr(>|t|)
(Intercept) 3.3333333  0.8397531 3.9694209 0.02857616
genderM     0.1666667  1.3277662 0.1255241 0.90804814
Bash

F学生的估计值为3.3333333,M学生的估计值为0.16666667。男学生和女学生的Pr值并不显著,只有0.90-0.02 ~ 0.9,也就是说,没有实际证据表明男学生比女学生呆的时间多。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程