You are on page 1of 4

set.

seed(421)

mtcars

str(mtcars)

wt=mtcars$wt

disp=(mtcars$disp-mean(mtcars$disp))/sd(mtcars$disp)

mpg=mtcars$mpg

traindata=cbind(wt,disp,mpg)

library(scatterplot3d)

scatterplot3d(wt, # x axis

disp, # y axis

mpg, # z axis

type="h",

color="red",

main="3-D Scatterplot",

xlab="Weight",

ylab="Displacement",

zlab="Mpg")

x1_test <- seq(from =-1.8, to = 2.25, by = 0.1275)

x2_test <- seq(from = -1.3, to = 2, by = 0.105)


x=cbind(1,wt,disp)

y=mpg

mvl_regression <- function(x, y) {

# calculate w

w <- solve(t(x) %*% x) %*% t(x) %*% y

return(w)

w<-mvl_regression(x,y)

print(w)

print(x)

print(y)

estimate=x%*%w

library(scatterplot3d)

s3d<-scatterplot3d(wt, # x axis

disp, # y axis

mpg, # z axis

type="h",

color="red",
main="3-D Scatterplot",

xlab="Weight",

ylab="Displacement",

zlab="Mpg"

s3d$points3d(wt,disp,estimate,col='blue' ,type='l',lwd=1)

A=cbind(x,wt^2,disp^2)

print(A)

w_plnm<-mvl_regression(A,y)

print(w_plnm)

estimate_plnm=A%*%w_plnm

s3d$points3d(wt,disp,estimate_plnm,col='green' ,type='l',lwd=1)

result=cbind(y,estimate,estimate_plnm)
mean_squared_linear=sum((y-estimate)^2)/length(y)

rms_linear=sqrt(mean_squared_linear)

mean_squared_plnm=sum((y-estimate_plnm)^2)/length(y)

rms_plnm=sqrt(mean_squared_plnm)

You might also like