1
votes

I have a list that has several columns. The leftmost column is my x axis data whilst the rest are various y data sets.

I want to produce linear regressions for each of those columns, but just for a specific range in x, then force the linear regression through 0,0. And finally extract the line slope of said linear regression. Below is an example of some of my data.

    x       y1       y2       y3       y4
1  2.495   -1e-04   -1e-03   -1e-04    0e+00
2  2.995   -2e-04   -7e-04   -2e-04    0e+00
3  3.495    2e-04   -2e-04    1e-04    2e-04
4  3.995    0e+00   -3e-04    0e+00    1e-04
5  4.495    0e+00   -3e-04    0e+00    0e+00
6  4.995    0e+00   -3e-04    0e+00    1e-04
7  5.495    1e-04   -2e-04    0e+00    1e-04
8  5.995   -1e-04   -1e-04    0e+00    3e-04
9  6.495    1e-04    1e-04    2e-04    3e-04
10 6.995    1e-04    0e+00    0e+00    3e-04

Here for example I would obtain linear regressions for the range in x of for example rows 3 to 10, coercing the regression to intersect 0,0. Finally the slopes from the linear regressions for y1, y2 etc are obtained.

I am aware of the abind and lm functions but have only been able to plot single regressions without coercing a 0,0 intersect. Thanks

1
Could you clarify which variables are your independent variables and which are your dependent? To force an intercept of 0, you simply add -1 to your regression model, e.g.lm(y ~ x -1). - paqmo
Column x is the independent variable, whilst y1, y2.. are dependent. - AGB

1 Answers

1
votes

A regression model without an intercept forces the regression line through the origin. This is achieved in R by adding -1 to the model. To get the range of x you desire, simply subset the observations to that range and use lapply to loop over the different independent variables:

dat2 <- dat[3:9,]
fits <- lapply(dat2[,-1], function(x) lm(x ~ dat2[,1] - 1))

Plotting the data and regression lines with ggplot2. Use melt from the reshape2 package to make the data long. This helps ggplot2 break the data in categories:

library(ggplot2); library(reshape2)
mdat <- melt(dat2,id="x")
ggplot(data = mdat) + 
  geom_point(aes(y = value, x = x, color=variable)) +
  geom_smooth(data=mdat,aes(x=x,y=value,color = variable),formula = y ~ x - 1,
              method = "lm", se = F)

enter image description here