r语言中的多因素方差分析_R中的因素
r语言中的多因素方差分析
In this tutorial, we’ll move on to understanding factors in R programming. One operation we perform frequently in data science is the estimation of a variable based upon the model we built. We are sometimes required to estimate the price of a share or a house, and sometimes we need to estimate what color car is likely to be sold the fastest.
在本教程中,我们将继续了解R编程中的因素。 我们在数据科学中经常执行的一项操作是根据我们建立的模型对变量进行估算。 有时我们需要估计股票或房屋的价格,有时我们需要估计最快销售哪种颜色的汽车。
Variables in data science fall under two categories – continuous and categorical. Continuous variables are those that can take numerical values including floating points. Prices of houses or shares, quantifiable variables like age, weight or height of a person are all continuous variables.
数据科学中的变量分为两类- 连续的和分类的 。 连续变量是那些可以采用包括浮点在内的数值的变量。 房屋或股份的价格,诸如年龄,体重或身高等可量化变量都是连续变量。
On the other hand, categorical variables take a set of fixed values that can be represented using a set of labels. Examples for this category as marital status, gender, the color of the vehicle, the highest educational degree of a person and so on.
另一方面,分类变量采用一组固定值,可以使用一组标签来表示。 例如,婚姻状况,性别,车辆的颜色,个人的最高学历等。
Categorical variables are represented using the factors in R.
分类变量使用R中的因子表示。
在R中创建因素 (Creating Factors in R)
Factors can be created using a factor()
function.
可以使用factor()
函数创建factor()
。
factor(x=vector, levels, labels, is.ordered=TRUE/FALSE)
The first argument to factor function is the vector x of values that you wish to factorize. Note that you cannot create a factor using a matrix. X should always be a single-dimensional vector of character strings or integer values.
因子函数的第一个参数是要分解的值的向量 x。 请注意,您不能使用矩阵创建因子。 X应该始终是字符串或整数值的一维向量 。
Secondly, you need to supply the list of levels you need in the factor. Levels is a vector of unique values used in the factor. This is an optional argument.
其次,您需要提供因子中所需级别的列表。 级别是因子中使用的唯一值的向量。 这是一个可选参数。
The third argument is labels. Sometimes when you encode the variables as a vector of integers, you need to specify what integer represents what label. You could use 0 and 1 to represent male and female, but you need to specify that using these labels. So basically this is the key for looking up the factors.
第三个参数是标签 。 有时,当您将变量编码为整数向量时,需要指定什么整数代表什么标签。 您可以使用0和1来代表男性和女性,但是您需要使用这些标签来指定。 因此,基本上,这是查找因素的关键。
Finally, you have a Boolean valued argument is.ordered. Sometimes you may wish to retain the order amongst the factors used. For example, you may encode the month of joining using integers 1 to 12, to represent months from January to Decemeber. In these cases, you need to specify ordered to TRUE.
最后,您有一个布尔值参数is.ordered 。 有时,您可能希望保留所使用因素之间的顺序。 例如,您可以使用整数1到12编码加入月份,以表示从一月到十二月的月份。 在这些情况下,您需要将命令指定为TRUE。
Let us look at examples of factors now.
现在让我们来看一些因素的例子。
#Encode the genders of people into a vector first
#These might be extracted from a dataset usually.
> genvector <- c("Male","Female","Female","Male","Male","Female")#Create a factor from this vector
> genfact <- factor(genvector)
> genfact
[1] Male Female Female Male Male Female
Levels: Female Male
Notice how the levels are automatically obtained from the vector’s unique values here. Let us try another example where we define male and female as 0 and 1 using labels.
请注意,此处是如何从向量的唯一值自动获取级别的。 让我们尝试另一个示例,其中使用标签将“男性”和“女性”定义为0和1。
#Define a vector with 0 for Male and 1 for Female.
> genvector2 <- c(0,1,1,0,0,1)
#Assign labels Male and Female to 0 and 1 when creating a Factor.
> genfact2 <-factor(genvector2,levels=c("0","1"),labels=c("Male","Female"))
> genfact2
[1] Male Female Female Male Male Female
Levels: Male Female
Observe that the labels you have defined are displayed instead of 0 and 1 defined in the factor.
请注意,显示的是您定义的标签,而不是因子中定义的0和1。
R编程中的因素排序 (Ordering in Factors in R Programming)
Let us work another example using the ordering of factor levels. Let us first define a vector representing the month of joining for 8 employees.
让我们使用因子水平的排序来工作另一个示例。 让我们首先定义一个向量,表示8位员工的加入月份。
> moj <- c("Jan","Jun","May","Jan","Apr","Dec","Nov","Sep")
Now, there is no way for the compiler to know that May comes before Jun in the order of months. So the following code throws FALSE.
现在,编译器无法知道May会在Jun之前几个月出现。 因此,以下代码将引发FALSE。
> moj[2]>moj[3]
[1] FALSE
To impose ordering, we need to define a vector with all the months in order first.
要强加排序 ,我们需要先定义一个包含所有月份的向量。
> ordermonths <-c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
Now create a factor for our data using our moj vector, set the levels to ordermonths and set the argument ordered to TRUE.
现在,使用moj向量为我们的数据创建一个因子,将级别设置为ordermonths并将参数定为TRUE。
> factormoj <- factor(x=moj, levels=ordermonths, ordered=TRUE)
Now factormoj displays as follows.
现在factormoj显示如下。
> factormoj
[1] Jan Jun May Jan Apr Dec Nov Sep
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < ... < Dec
The compiler now knows the ordering among the months. Let us check if it knows that May comes before June.
现在,编译器知道月份之间的顺序。 让我们检查一下是否知道五月份会在六月之前。
> factormoj[2]>factormoj[3]
[1] TRUE
修改因素 (Modifying Factors)
Each element of factor can be assigned a value individually using indexing, just like we index vectors. Let us modify a value from the genfactor we created earlier in the tutorial.
就像我们对向量进行索引一样,可以使用索引分别为因子的每个元素分配一个值。 让我们根据本教程前面创建的基因因子修改值。
We’ll continue with the same variable from before, genfact to make things easier for you.
我们将继续使用以前相同的变量,通过genfact为您简化事情。
> genfact
[1] Male Female Female Male Male Female
Levels: Female Male
> genfact[1]
[1] Male
Levels: Female Male
> genfact[1]<-"Female"
> genfact
[1] Female Female Female Male Male Female
Levels: Female Male
为因素添加新的水平 ( Adding New Levels to Factors )
To add a new level to a factor, which hasn’t been defined earlier, you just need to modify the levels vector in the following manner. Let’s try this on our existing genfact variable.
要将新级别添加到因子(之前尚未定义),您只需按照以下方式修改级别向量即可。 让我们在现有的genfact变量上尝试一下。
> levels(genfact) <- c(levels(genfact),"Other")
> genfact
[1] Female Female Female Male Male Female
Levels: Female Male Other
You can now modify the factors to the newly defined level “Other” as well.
现在,您也可以将因子修改为新定义的级别“其他”。
> genfact[3] <- "Other"
> genfact
[1] Female Female Other Male Male Female
Levels: Female Male Other
翻译自: https://www.journaldev.com/35599/factors-in-r
r语言中的多因素方差分析
r语言中的多因素方差分析_R中的因素相关推荐
- R语言实战笔记--第九章 方差分析
R语言实战笔记–第九章 方差分析 标签(空格分隔): R语言 方差分析 术语 组间因子,组内因子,水平:组间因子和组同因子的区别是,组间因子对所有测试对象进行分组,而组内因子则把所有测试对象归为同一组 ...
- R语言ggplot2可视化:使用ggfortyify包中的autoplot函数自动可视化时间序列数据(Time Series Plot From a Time Series Object (ts))
R语言ggplot2可视化:使用ggfortyify包中的autoplot函数自动可视化时间序列数据(Time Series Plot From a Time Series Object (ts)) ...
- R语言ggplot2可视化:在可视化图像中添加对角线(diagonal line)
R语言ggplot2可视化:在可视化图像中添加对角线(diagonal line) 目录 R语言ggplot2可视化:在可视化图像中添加对角线(diagonal line)
- R语言编写自定义函数自定义ggplot图像中的图例(legend)的位置、图例标题、键值、文本字体大小(title、text、key)、颜色标识的大小、点形状pch的大小
R语言编写自定义函数自定义ggplot图像中的图例(legend)的位置.图例标题.键值.文本字体大小(title.text.key).颜色标识的大小.点形状pch的大小 目录
- R语言使用anova函数进行方差分析比较两个回归分析模型的差异、从而决定是否删除某些预测变量(Comparing nested models using the anova function)
R语言使用anova函数进行方差分析比较两个回归分析模型的差异.从而决定是否删除某些预测变量(Comparing nested models using the anova function) 目录
- R语言ggplot2可视化、在可视化区域中自定义添加多个大小不同矩形阴影区域、自定义配置大小不同矩形阴影区域的颜色(Adding multiple shadows/rectangles)
R语言ggplot2可视化.在可视化区域中自定义添加多个大小不同矩形阴影区域.自定义配置大小不同矩形阴影区域的颜色(Adding multiple shadows/rectangles) 目录
- R语言stringr包str_detect函数检测字符串中模式存在与否实战
R语言stringr包str_detect函数检测字符串中模式存在与否实战 目录 R语言stringr包str_detect函数检测字符串中模式存在与否实战 #导入stringr包
- R语言自定义函数计算dataframe每列中的缺失值NA的个数、缺失值问题及其填充示例
R语言自定义函数计算dataframe每列中的缺失值NA的个数.缺失值问题及其填充示例 目录
- R语言ggplot2可视化在可视化的接种中插入图片、添加图片实战
R语言ggplot2可视化在可视化的接种中插入图片.添加图片实战 目录 R语言ggplot2可视化在可视化的接种中插入图片.添加图片实战
最新文章
- 魔法一样隔空在屏幕写字,捏起手指就能实现!在线可玩
- could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
- OpenStack发布最新版本Ocata
- Python 技术篇 - 修改源码解决中文主机名导致的flask、socket服务起不来问题: ‘utf-8‘ codec can‘t decode byte 0xc0 in position...
- android捕获线程异常,android - 终结器引发的未捕获的异常:所有WebView方法必须在同一线程上调用。 (预期的Looper) - 堆栈内存溢出...
- python怎么解释语言_python是解释型语言吗
- vue从入门到精通之进阶篇(一)vue-router基础
- ribbon源码(1) 概述
- 小米岭南通服务器维护,小米岭南通交通联合卡内测开启
- WinForm与脚本的交互
- java gc机制新区域旧屋_Java 内存回收机制——GC机制-Go语言中文社区
- 【Java从入门到头秃专栏 】(一)学在Java语法之前
- css3图像边框:border-image - 代码篇
- JVM优化系列-Stop-The-World实战
- Android 打包报错 Error:(3) Error: jdjg_str is not translated in zh (Chinese) [MissingTranslation]
- Linux下mysql主从复制配置(CentOS7)
- ubuntu16.04 apt-get update出错:由于没有公钥,无法验证下列签名
- HTTP 缓存机制及原理
- html 调用离线地图,百度地图API1.1制作的离线地图控件(html+webbroswer)
- java程序启动端口_查看项目端口和启动情况