指定回归线截距(R& ggplot2) [英] Specify regression line intercept (R & ggplot2)
问题描述
我现在的情节是这样的:
问题
我想要强制回归线从station_1开始1。
CODE
library(ggplot2)
#READ IN DATA
var_x = c(2001,2002,2003,2004,2005,2007,2007,2008,2009 ,2010,2011,2001,2002,2003,2004,2005,2007,2008,2009,2010,2011)
var_y = c(1.000000,1.041355,1.053106,1.085738,1.126375,1.149899,1.210831,1.249480 ,1.286305,1.367923,1.486978,1.000000,0.9849343,0.9826141,0.9676000,0.9382975,0.9037476,0.8757748,0.8607960,0.8573634,0.8536138,0.8258877)
var_z = c('Station_1','Station_1','Station_1',' Station_1' , 'Station_1', 'Station_1', 'Station_1', 'Station_1','为static on_1' , 'Station_1', 'Station_1', 'Station_2', 'Station_2', 'Station_2', 'Station_2', 'Station_2', 'Station_2', 'Station_2', 'Station_2', 'Station_2', 'Station_2' ,'Station_2')
df_data = data.frame(var_x,var_y,var_z)
$ b $ out = ggplot(df_data,aes(x = var_x,y = var_y,group = var_z))
out = out + geom_line(aes(linetype = var_z),size = 1)
out = out + theme_classic()
# b $ b $ PFI_data =子集(df_data,var_z ==Station_1)
#PLOT REGRESSION FOR Station_1
out = out + stat_smooth(data = PFI_data,
method = lm,
formula = y〜x,
se = T,size = 1.4,color =blue,linetype = 1)
任何帮助将不胜感激 - 这已经让我疯狂了太久!
首先,在对某些固定p进行回归时,应该小心oint。这里有一个BACKGROUND
My current plot looks like this:
PROBLEM
I want to force the regression line to start at 1 for station_1.
CODE
library(ggplot2)
#READ IN DATA
var_x = c(2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011)
var_y = c(1.000000,1.041355,1.053106,1.085738,1.126375,1.149899,1.210831,1.249480,1.286305,1.367923,1.486978,1.000000,0.9849343,0.9826141,0.9676000,0.9382975,0.9037476,0.8757748,0.8607960,0.8573634,0.8536138,0.8258877)
var_z = c('Station_1','Station_1','Station_1','Station_1','Station_1','Station_1','Station_1','Station_1','Station_1','Station_1','Station_1','Station_2','Station_2','Station_2','Station_2','Station_2','Station_2','Station_2','Station_2','Station_2','Station_2','Station_2')
df_data = data.frame(var_x,var_y,var_z)
out = ggplot(df_data,aes(x=var_x,y=var_y,group=var_z))
out = out + geom_line(aes(linetype=var_z),size=1)
out = out + theme_classic()
#SELECT DATA FOR Station_1
PFI_data=subset(df_data,var_z=="Station_1")
#PLOT REGRESSION FOR Station_1
out = out+ stat_smooth(data = PFI_data,
method=lm,
formula = y~x,
se=T,size = 1.4,colour = "blue",linetype=1)
Any help would be appreciated - this has been driving me crazy for too long!
First of all, you should be careful when forcing a regression line to some fixed point. Here's a link to a discussion why.
Now, from a technical perspective, I'm relying heavily on these questions and answers: one, two. The outline of my solution is the following: precompute the desired intercept, run a regression without it, add the intercept to the resulting prediction.
I'm using an internal ggplot2:::predictdf.default
function to save some typing. The cbind(df, df)
part may look strange, but it's a simple hack to make geom_smooth
work properly, since there are two factor levels in var_z
.
# Previous code should remain intact, replace the rest with this:
# SELECT DATA FOR Station_1
PFI_data=subset(df_data,var_z=="Station_1")
names(PFI_data) <- c("x", "y", "z")
x0 <- df_data[df_data$var_z == "Station_1", "var_x"][1]
y0 <- df_data[df_data$var_z == "Station_1", "var_y"][1]
model <- lm(I(y-y0) ~ I(x-x0) + 0, data = PFI_data)
xrange <- range(PFI_data$x)
xseq <- seq(from=xrange[1], to=xrange[2])
df <- ggplot2:::predictdf.default(model, xseq, se=T, level=0.95)
df <- rbind(df, df)
df[c("y", "ymin", "ymax")] <- df[c("y", "ymin", "ymax")] + y0
out + geom_smooth(aes_auto(df), data=df, stat="identity")
这篇关于指定回归线截距(R& ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!