sankara.deva2016@gmail.com
bharat sreeram, Ventech It Solutions.
-----------------------------------------------------------
# Linear REgression Implementation in R
---------------
df =
read.csv("C:/Users/Hadoop/Desktop/prof.txt")
> df
a w h c
1 25 80 5.9 120
2 23 55 5.7 90
3 23 89 6.0 130
4 26 80 5.9 120
5 23 55 5.7 90
6 23 69 6.0 130
7 28 81 5.9 120
8 23 55 5.9 190
9 23 81 6.0 130
10 29 87 5.9 120
11 23 55 5.7 190
12 23 89 5.0 130
>
Y = matrix(df$c, ncol=1)
Xdf = data.frame(beta = 1, a=df$a, w=df$w,
h=df$h)
X = data.matrix(Xdf)
Xt = t(X)
XtX = Xt %*% X
inv = solve(XtX)
XtY = Xt %*% Y
beta = inv %*% XtY
-----------------------
model = lm(c ~ a + w + h , data=df)
-----------------------
Model testing: [accuracy testing ]
> x
[1] 10 20 30 40 23 45 67 90 100 150
> y
[1] 23 45 67 90 50 100 130 190 210 270
> cor(x,y)
[1] 0.9929889
> df = data.frame(x, y)
> lmfit = lm(y ~ x, data=df)
> params = coefficients(lmfit)
> a = params[1]
> b = params[2]
> test = data.frame(y=df$y)
> test$ycap = a + (b*df$x)
> test$dist = ((test$ycap-test$y)*100)/test$y
> test$res = "Fail"
> test$res[test$dist>=-20 & test$dist<=20] = "Pass"
> prop.table(table(test$res))
--------------------------------
non-linear regression implementation:
> dframe = data.frame(x = df$x,
+ xsq = df$x^2,
+ xcube = df$x^3, y = df$y)
> nlmfit = lm(y ~ x + xsq + xcube, data=dframe)
> p = coefficients(nlmfit)
> b0 = p[1]
> b1 = p[2]
> b2 = p[3]
> b3 = p[4]
> newtest = data.frame(y = dframe$y)
> newtest$ycap = b0 + (b1*dframe$x) +
+ (b2 * dframe$xsq) +
+ (b3 * dframe$xcube)
> newtest$dist = ((newtest$ycap-newtest$y)*100)/newtest$y
> newtest$res = "Fail"
> newtest$res[newtest$dist>=-20 &
newtest$dist<=20] = "Pass"
> prop.table(table(newtest$res))
---------------
df =
read.csv("C:/Users/Hadoop/Desktop/prof.txt")
> df
a w h c
1 25 80 5.9 120
2 23 55 5.7 90
3 23 89 6.0 130
4 26 80 5.9 120
5 23 55 5.7 90
6 23 69 6.0 130
7 28 81 5.9 120
8 23 55 5.9 190
9 23 81 6.0 130
10 29 87 5.9 120
11 23 55 5.7 190
12 23 89 5.0 130
>
Y = matrix(df$c, ncol=1)
Xdf = data.frame(beta = 1, a=df$a, w=df$w,
h=df$h)
X = data.matrix(Xdf)
Xt = t(X)
XtX = Xt %*% X
inv = solve(XtX)
XtY = Xt %*% Y
beta = inv %*% XtY
-----------------------
model = lm(c ~ a + w + h , data=df)
-----------------------
Model testing: [accuracy testing ]
> x
[1] 10 20 30 40 23 45 67 90 100 150
> y
[1] 23 45 67 90 50 100 130 190 210 270
> cor(x,y)
[1] 0.9929889
> df = data.frame(x, y)
> lmfit = lm(y ~ x, data=df)
> params = coefficients(lmfit)
> a = params[1]
> b = params[2]
> test = data.frame(y=df$y)
> test$ycap = a + (b*df$x)
> test$dist = ((test$ycap-test$y)*100)/test$y
> test$res = "Fail"
> test$res[test$dist>=-20 & test$dist<=20] = "Pass"
> prop.table(table(test$res))
--------------------------------
non-linear regression implementation:
> dframe = data.frame(x = df$x,
+ xsq = df$x^2,
+ xcube = df$x^3, y = df$y)
> nlmfit = lm(y ~ x + xsq + xcube, data=dframe)
> p = coefficients(nlmfit)
> b0 = p[1]
> b1 = p[2]
> b2 = p[3]
> b3 = p[4]
> newtest = data.frame(y = dframe$y)
> newtest$ycap = b0 + (b1*dframe$x) +
+ (b2 * dframe$xsq) +
+ (b3 * dframe$xcube)
> newtest$dist = ((newtest$ycap-newtest$y)*100)/newtest$y
> newtest$res = "Fail"
> newtest$res[newtest$dist>=-20 &
newtest$dist<=20] = "Pass"
> prop.table(table(newtest$res))
No comments:
Post a Comment