我正在尝试在分组数据中进行线性拟合。
但是,我还想在整个拟合过程中添加一些条件,但是当我对某些条件进行子集化时没有这样做。
set.seed(183)
library(dplyr)
V <- rep(seq(1,8),3)
value = c(c(sort(runif(5,0.001,1)),rep(0,3)),c(sort(runif(5,0.001,1)),rep(0,2),runif(1,0.001,1)),c(sort(runif(5,0.001,1)),rep(0,2),runif(1,0.001,1)))
group=rep(letters[1:3],each=8)
df <- data.frame(group,V,value)
# > df
# group V value
#1 a 1 0.15087459
#2 a 2 0.35408406
#3 a 3 0.47339320
#4 a 4 0.67614665
#5 a 5 0.98273932
#6 a 6 0.00000000
#7 a 7 0.00000000
#8 a 8 0.00000000
#9 b 1 0.32821476
#10 b 2 0.35737009
#11 b 3 0.58821689
#12 b 4 0.81088053
#13 b 5 0.99122633
#14 b 6 0.00000000
#15 b 7 0.00000000
#16 b 8 0.03697432
#17 c 1 0.12940226
#18 c 2 0.41918905
#19 c 3 0.66020739
#20 c 4 0.84124155
#21 c 5 0.95052213
#22 c 6 0.00000000
#23 c 7 0.00000000
#24 c 8 0.15071444
我在每个组内的条件是
1)如果所有最后 3 个value==0
仅在V>=4&V<=5
2)如果最后 2value>0
只适合V>=7
.
这是我为执行此操作而编写的函数
get_slope <- function(df){
if (tail(df$value,3)==0)
slp = coef(lm(value~V, data=subset(df,V>=4&V<=5)))[2]
else
if (any(tail(df$value,3)>=0))
slp = coef(lm(value ~ V, data=subset(df,V>=7)))[2]
return(slp)
}
df_slope <- df%>%
group_by(group)%>%
do(.,slope=get_slope(df))
Warning messages:
1: In if (tail(df$value, 3) == 0) slp = coef(lm(value ~ V, data = subset(df, :
the condition has length > 1 and only the first element will be used
2: In if (tail(df$value, 3) == 0) slp = coef(lm(value ~ V, data = subset(df, :
the condition has length > 1 and only the first element will be used
3: In if (tail(df$value, 3) == 0) slp = coef(lm(value ~ V, data = subset(df, :
the condition has length > 1 and only the first element will be used
最后我想得到每组的斜率值。
是否有捷径可寻?
提前谢谢了!