The regular lm function has a weights argument, which you can use to assign weight to a specific observation. Thus, you can understand what effect it has on the result. I think this is a general way to solve the problem instead of a subset of the data. Of course, the use of ad hoc weights does not bode well for the statistical validity of the analysis. It is always better to have a rationale behind the scales, for example. Low weight observations have higher uncertainty.
I think the lm function is used under the hood of ggplot2 so that you can pass the weights argument. You can add scales through aesthetic ( aes ), assuming that the weight is stored in the vector:
ggplot(data,aes(x=x,y=y,colour=factor(colour))) + geom_point()+ stat_smooth(aes(weight = runif(nrow(data))), method='lm')
you can also put the weight in a column in the dataset:
ggplot(data,aes(x=x,y=y,colour=factor(colour))) + geom_point()+ stat_smooth(aes(weight = weight), method='lm')
where the column is called weight .
Paul hiemstra
source share