如何对数据帧的每一行执行线性回归
我有一个包含数千行的数据框,但为了简单起见,我们假设它有 10 行。考虑测量几名患者的十种不同蛋白质,平均值列在以下数据框中。
proteins Year.1 Year.2 Year.4 Year.5
1 p1 1.90 2.30 2.40 2.80
2 p2 0.90 1.20 1.50 1.90
3 p3 2.30 5.20 6.20 8.70
4 p4 2.10 2.20 2.50 2.60
5 p5 1.85 1.92 1.99 2.01
6 p6 1.20 1.45 1.55 1.65
7 p7 3.50 3.60 3.80 4.10
8 p8 4.20 5.60 6.50 7.20
9 p9 3.80 3.90 4.10 4.50
10 p10 23.00 4.20 6.50 8.90
我需要一个 r 代码来对每一行运行线性回归(例如行 i=1: x=(1,2,3,4), y=(year.1[i,],year.2[i,] ,年.3[i,],年.4[i,])) 并创建几个列,其中可以记录它们的截距、斜率、Rsquared。
我对 R 很陌生,已经做了一些研究,但不知道如何编写 lm 函数的公式
fold_model_lm<-function(df) {
lm((x<-c(1,2,3,4))~(y<-c(year.1,year.2,year.3,year.4)), data=df)
}
,但它不起作用。知道如何做到这一点吗?
I have a data frame containing thousands of rows but for simplicity let's consider it has 10 rows. Consider ten different proteins for several patients were measured, and the average is listed in the following dataframe.
proteins Year.1 Year.2 Year.4 Year.5
1 p1 1.90 2.30 2.40 2.80
2 p2 0.90 1.20 1.50 1.90
3 p3 2.30 5.20 6.20 8.70
4 p4 2.10 2.20 2.50 2.60
5 p5 1.85 1.92 1.99 2.01
6 p6 1.20 1.45 1.55 1.65
7 p7 3.50 3.60 3.80 4.10
8 p8 4.20 5.60 6.50 7.20
9 p9 3.80 3.90 4.10 4.50
10 p10 23.00 4.20 6.50 8.90
I need an r code to run the linear regression over each row (e.g row i=1: x=(1,2,3,4), y=(year.1[i,],year.2[i,],year.3[i,],year.4[i,]))
and create several column where the intercept, slope, Rsquared can be recorded for them.
I am very new to R and have done some research but not sure how to write the formula for lm function
fold_model_lm<-function(df) {
lm((x<-c(1,2,3,4))~(y<-c(year.1,year.2,year.3,year.4)), data=df)
}
but it did not work. Any idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新以提取 r 平方,并放弃使用
broom::tidy
输出:
先前的答案
您可以使用 tidyverse 和 broom
输出:
输入:
Updated to extract r-squared, and to forego the use of
broom::tidy
Output:
Prior Answer
You can use tidyverse and broom
Output:
Input: