0
$\begingroup$

I've been trying to get this code to work for a spline regression without using built in libraries in R. My code below is resulting in the error
Error in eval(predvars, data, env) : object 'V1' not found

Also I want to actually plot the spline curve on the data but not sure how to proceed with that. Any help would be greatly appreciated.

##Spline regression##
x=runif(100,0,10)
y=cos(x)+ .25*rnorm(100)
plot(x,y)
df=data.frame(cbind(x,y))
colnames(df)=c('x','y')

degree=3
knots=c(2,4,6,8)
d1=outer(df$x,1:degree,"^")
d2=outer(df$x,knots,">")*
  outer(df$x,knots,"-")^degree
df=as.data.frame(cbind(d1,d2,y))
model = lm(y ~.,data=df)
new <- data.frame(x = seq(0, 10, 0.1))
y_new <- predict(model, newdata=new)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.