简体   繁体   English

如何将矩阵的仅一行转换为向量,以便可以对其进行线性回归?

[英]How to turn just one row of a matrix into a vector so I can do a linear regression on it?

I'm trying to do my first ever project in R and I just don't know the language, so it's really killing me here. 我正在尝试在R中做我的第一个项目,但是我不懂这种语言,所以在这里真的使我丧命。 This is the most frustrating thing I've ever encountered, mostly because it seems like there is absolutely nowhere on the internet that caters to people who don't know the language to teach you how to do things. 这是我遇到过的最令人沮丧的事情,主要是因为似乎互联网上绝对没有地方可以迎合那些不懂得如何教您如何做事的人。

I am trying to run a linear regression with the data that I'm using being one of the built-in datasets that RStudio has. 我正在尝试将所使用的数据作为RStudio的内置数据集之一进行线性回归。 This is my line of code: 这是我的代码行:

    lm(Income ~ Illiteracy, data=florida)

But I keep coming up with this error: 但是我一直想出这个错误:

Error in model.frame.default(formula = Income ~ Illiteracy, data = florida,: 'data' must be a data.frame, not a matrix or an array model.frame.default中的错误(公式=收入〜文盲,数据=佛罗里达,:“数据”必须是data.frame,而不是矩阵或数组

(friend who was helping me renamed state.x77 into "florida"). (帮助我的朋友将state.x77重命名为“佛罗里达”)。

After getting this error and deciding that I would prefer to either do each state individually in the regression or at least a couple sample states, I decided I wanted to take the Florida row and turn it into its own vector to do the analysis on. 收到此错误并决定我希望在回归中分别对每个状态进行分析,还是希望至少对几个样本状态进行分析后,我决定我要对Florida行进行分析并将其转换为自己的向量。 However, I have NO idea how to do that. 但是,我不知道该怎么做。 I keep seeing suggestions on this website but they're all taking about "naming" things and a lot of the commands have "dim" which no one explains. 我在该网站上不断看到建议,但是它们都是关于“命名”的事情,许多命令都带有“暗淡”的字眼,没有人解释。

Please help I'm a total beginner and I have a textbook that assumes you know R and I found another "Learn R" book that somehow also assumes you know R 请帮助我,我是一个完全的初学者,我有一本教科书,假设您知道R,而我又找到了一本“学习R”的书,以某种方式也假定您知道R

R has several data structures for handling datasets. R具有用于处理数据集的几种数据结构。 A matrix is one of them - it restricts you to a single type of variable (usually numeric ), and must have a rectangular shape. matrix就是其中之一-它将您限制为单一类型的变量(通常为numeric ),并且必须具有矩形形状。

A data.frame is similar in shape to a matrix, but each column can be a different type (eg numeric , character , or factor ). data.frame形状类似于矩阵,但是每一列可以是不同的类型(例如, numericcharacterfactor )。 This is closer to a typical dataset, where you have a mixture of continuous / numeric, ordinal, and categorical / nominal variables. 这更接近于典型的数据集,在该数据集中混合了连续/数字,序数和分类/名义变量。

You can check what sort of input a function requires by typing ?functionname , eg ?lm and inspecting the Arguments section: 您可以通过输入?functionname (例如?lm并检查Arguments部分来检查功能需要哪种输入:

data 数据
an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. 包含模型中变量的可选数据框,列表或环境(或可通过as.data.frame强制转换为数据框的对象)。 If not found in data, the variables are taken from environment(formula), typically the environment from which lm is called. 如果在数据中找不到,则变量取自环境(公式),通常是调用lm的环境。

Before experimenting with regression, you can learn the basic building blocks of R with a good introductory course. 在尝试回归之前,您可以通过良好的入门课程来学习R的基本构成部分。 One free option is DataCamp's Introduction to R , but there are many others. 一个免费的选项是DataCamp的R简介 ,但还有很多其他选项。 Once you understand the different variable types, data structures, and syntax of R, these errors are easy to correct. 一旦了解了R的不同变量类型,数据结构和语法,这些错误就很容易纠正。

In this case, you just need to write as.data.frame(florida) to "coerce" the matrix object to a data.frame object. 在这种情况下,您只需要编写as.data.frame(florida)即可将matrix对象“强制”为data.frame对象。

If you want to get a model for each state try this 如果要获取每个州的模型,请尝试以下操作

data(state)

state.x77 <- as.data.frame(state.x77)
state.x77$name <- rownames(state.x77)
mod_list <- list()
for (s in unique(rownames(state.x77))) {
    m <- lm(Income ~ Illiteracy, data = subset(state.x77, name == s))
    mod_list <- c(mod_list, list(mod = m))
}
names(mod_list) <- unique(rownames(state.x77))

For linear regression of Illiterace to Income, you should do: 为了使文盲到收入线性回归,您应该执行以下操作:

lm(Income ~ Illiteracy, data=as.data.frame(state.x77))

because lm accepts dataframes, not matrices. 因为lm接受数据帧,而不是矩阵。

friend who was helping me renamed state.x77 into "florida" 帮助我的朋友将state.x77重命名为“佛罗里达”

I don't know why would he or she do it. 我不知道他或她为什么要这么做。 state.x77 is a data of 8 parameters for 50 different states. state.x77是50个不同状态的8个参数的数据。 Florida is just one of them, so why on the earth would he call it "florida"? 佛罗里达只是其中之一,所以为什么他在地球上会称其为“佛罗里达”? Suppose you have a dataset of population and income of 200 different countries. 假设您有一个200个不同国家的人口和收入数据集。 Would you call it "india" because India is one of the countries in the dataset? 因为印度是数据集中的国家之一,您会称其为“印度”吗?

After getting this error and deciding that I would prefer to either do each state individually in the regression 收到此错误并决定我宁愿在回归中分别做每个状态

You cannot "do a state individually in the regression". 您不能“在回归中单独设置状态”。 Not that you cannot do it in R, you cannot do it at all, because it is mathematically absurd. 并不是说您不能在R中做到这一点,您根本无法做到,因为它在数学上是荒谬的。 Florida has (in this matrix) a population of 4815 and an illiteracy of 1.3. 佛罗里达州(按此矩阵)人口为4815,文盲率为1.3。 How you do a regression between two numbers? 您如何在两个数字之间进行回归? It is absurd. 这很荒谬。

I decided I wanted to take the Florida row and turn it into its own vector to do the analysis on. 我决定要选择佛罗里达行并将其转换为自己的向量以进行分析。

You can take the Florida row: 您可以参加佛罗里达行:

foo <- state.x77["Florida",]

Now foo is the vector of 8 parameters for Florida, but what can you do with it? 现在foo是Florida的8个参数的向量,但是您可以使用它做什么呢?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何对数据集进行线性回归并将其逐列回归到一个向量? - How do we do linear regression on a dataset and regress it to one vector column by column? 我如何 output 一个矩阵向量或 dataframe ,其中每个元素都是从每一行连接的字符串? - How do I output one vector for a matrix or dataframe in which each element is a string concatenated from each row? 如何使用向量作为我的预测变量来运行多元线性回归? - How do I run a multiple linear regression using a vector as my predictors? 在给定协方差矩阵和拟合系数的情况下,如何计算线性回归的p值 - How do I calculate p values of a linear regression given the covariance matrix and fit coefficients 如何在 R 的线性回归中使用矩阵列的平均值作为预测值? - How do I use the means of the columns of a matrix as prediction values in a linear regression in R? 如何进行线性回归? - How to do this linear regression? 如何为这个线性 model 构建回归? - How can I build a regression for this linear model? 在R中运行线性回归时如何删除一行数据? - How to remove one row of data when running a linear regression in R? 如何为我的 3 个图在一张图中拟合一条线性回归线? - How can I fit one linear regression line in one graph for my 3 plots? 我如何预测我的线性元回归中的调节变量之一? - How do i predict one of the moderator variables in my linear meta-regression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM