Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(plotly)
- library(dplyr)
- Non_Lin_Regressie <- function(x,y){
- # Solving the least squares method
- Pol_Matrix <- matrix(c(x^2,x,rep(1,length(x))),ncol=3)
- LHS_pol <- t(Pol_Matrix) %*% Pol_Matrix # Computes the LHS
- RHS_pol <- t(Pol_Matrix) %*% y # Computes the RHS
- x_sol_pol <- solve(LHS_pol,RHS_pol) # Solves the equation
- return(x_sol_pol)
- }
- x <- c(-1,-1/2,0,1/2,1)
- y<- c(2,1,0,1,2)
- sol <- Non_Lin_Regressie(x,y)
- func <- sol[1]*x^2 + sol[2]*x+sol[3]
- plot_ly(x=~x, mode= 'lines') %>%
- add_trace(y = ~func, line = list(shape = "spline"), name= 'solved by LS') %>%
- add_trace(y=~y, name= 'normal plot')
- [,1]
- [1,] 1.7142857
- [2,] 0.0000000
- [3,] 0.3428571
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement