问题描述
例如,我要回归这样的模型:
这是我按组进行回归的数据和方法:
library(tidyverse)
library(plyr)
# create data
df = data.frame(y=c(1,3,2,5,6,8,7,9,10,15),x=c(1,12),j=c("type1","type1","type2","type3","type3"))
# regress
linear_fit <- function(df) {
model <- lm(y ~ x,df)
linear_coef <- coef(model)
linear_coef <- data.frame(intercept = linear_coef[1],slope = linear_coef[-1])
row.names(linear_coef) <- NULL
linear_coef
}
outcome<-ddply(df,.(j),linear_fit)
outcome
j intercept slope
1 type1 -0.500000 1.5000000
2 type2 3.328358 0.5074627
3 type3 -10.071429 2.0714286
但是问题是,通过这种方法,我不能使截距保持不变,只能改变斜率。我想知道如何使方程式回归,如图所示,该方程只有一个截距但具有根据“ j”列的不同斜率。
有人可以帮我吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)