I want to plot a spline with R. But I have the error message "'newdata' has 38 rows, but the variables found have 700 rows" I am not sure why a spline does has so many rows. It has indeed to many.
My professor told me that I should work with the functions lm() and bs(). That is why I am using them.
I hope for your help :-)
Bonus-Question: Can I plot 3D how all three variables: sales, visits and discount are connected with each other?
--
This here is my code. I hope that I am not missing anything. I am still missing the concept how to make out of a spline-model (spline_model) a line. And why do I need to predict the data again (ChatGPT told me that and some sources in the internet) with seq and pred. Why can I not use the normal spline_model for plotting?
library(dplyr) #Datenmanipulation
library(DataExplorer) #DatenExplore
library(skimr) #Datenüberblick
library(lmtest) #Regression
library(olsrr)
library(jtools)
library(moments)
library(highcharter) #Diagramme keine gewerbliche Nutzung
library(ggstatsplot)
library(ggplot2)
library(graphics)
library(mgcv) #spline
library(ggeffects) #GAM <- Spline
library(DiagrammeR) #Kausalitätsdiagramm
library(splines) #Splines
data_path <- "https://raw.githubusercontent.com/juanitorduz/website_projects/master/data/sales_dag.csv"
data <- read.csv(data_path)
data %>% select(visits, discount, sales) -> data_clean
spline_model <- lm(sales ~ bs(data_clean$visits, df = 3, knots = 3)+ data_clean$discount -1, data = data_clean) #spline
visits_range <- range(data_clean$visits) #MinMax der Var
visits_seq <- seq(from=visits_range[1], to=visits_range[2]) #Sequenz von Min nach Max neu
spline_model_pred<-predict(spline_model, newdata = data.frame(visits=visits_seq),se=T) #Vorhersage neu berechnen
plot(data_clean$visits, data_clean$sales, main="Beobachtete vs. Vorhergesagte Werte", xlab="Visits (IV)", ylab="Sales (DV)")
lines(visits_seq, spline_model_pred$fit, col = "red", lwd = 3)


Update. Everything solved with your help.
This is my current result