Regression line plotting problem

Hello,i'm trying to draw a regression line with my dataframe composed of x and y values ,values of x is in the range -2 and 6 and y values is between -0.5 and 1, since there are cosines and sines in it ,i tried to solve the regression problem as a polynomial but when i tried to plot the line ,line showed up in only -1 and 1 range but i want line to take values between -2 and 6, xlim sadly does not work.
here is the code i have written
df<-read.table("dataxy.csv",header = T)
x<-df[,1]
y<-df[,2]
quadmodel<-lm(y~cos(x), data=df)

a<-quadmodel$coefficients["cos(x)"]
c<-quadmodel$coefficients["(Intercept)"]

xp<-predict.lm(quadmodel,df)
yp<-a*cos(xp)+c
plot(x,y)
lines(xp,yp,col="green")

and this is the result i have got

I think you want this. The prediction will be a prediction for y, not x.

yp <- predict(quadmodel, df)
plot(x, y)
lines(x, yp, col = "green")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.