This is different, follow up question to someone else's question here:
I am very new to R and have no programming experience what so ever but I am holding my own for the data analysis we need in my lab. I am detecting changepoints in physiological data using cpt.mean() and until now I have been doing two separate linear regressions with lm(). One for the first half of my data on each sideand another for my second half of my data. I determine where to divide up the data after cpt.mean() tells me my changepoint. To make sureThe first line would normally have a slope around 0 and the lines connectedother could be anywhere from 0.2 to 2. I was finding the intercept ofwould then add those lines separately to my two best fit liesgraph and clippingclip them up to and from their intersection so that it was a smooth break point in what looked like a continuous "checkmark"appeared that I only had only line graphed but that that line had a "corner." At first it worked really well with one study and the intersections were very close to the changepoints cpt.mean() found. Now we are on a study that has data that isn't as clean andbut the intersection ofpoint where the two lines is very farjoined together was often further from the changepoint found with cpt.mean because what I graphed was determined by where the two equations mathematically intersected.
So I thought it would be better to find linear regression lines that anchor to the changepoint. That way when I graph two seperate lines, they connect at the changepoint that cpt.mean. I have read and understand the basic solution to this question linked above but I cant seem to figure out how to limit which data it looks at for the linear regression so that it only fits half of the data. Normally I do something like this if
In a made up scenario my changepoint was thecould be my 26th data point and(10, 20)
Normally I needed to perform a linear regressiondo something like this for only the first 26half of the data points: (PP is my X and MAP is my Y variable):
lm(MAP[1:26]~PP[1:26])
The simple solution to anchoring the best fit line to a specific point,(X0, Y0) was this
lm(I(y-y0)~I(x-x0) + 0)
with my variables and changepoint:
lm(I(MAP-y020)~I(PP-x010) + 0)
I figured I could just combine the two as follows:
lm(I(MAP[1:26]-y020)~I(PP[1:26]-x010) +0)
No error came up but nothing showed up on my scatter plot. Not sure whats happening.
My data has a "baseline" and then thereHere is a clear increase in the slope of the data. I performed a change point analysis to find where the data started increasing rapidly. Normally I take all of the data to the left of the changepoint and do linear regression. Then I take all the data to the rightan example of the changepoint and do another regression. I then take the two equations and plot them separately but I clip the lines so that it appears to have one line that has a "corner" or "elbow". Both options Glen_b listed seem appealing. I am mostly concerned with the x value.what my graphs normally look like:
Best, Ian