Pages

Thursday, 7 February 2013

BUSINESS APPLICATION LAB SESSION-5

Assignment -1:  Find returns of NSE data of greater than 6 months having selected the 10th data point as start and 95th data point as end. Also plot return.

Solution:
Command Sequence
> data<-read.csv(file.choose(),header=T)
> close<-data$Close[10:95]
> close.ts<-ts(close,deltat=1/252)
> close.ts
Time Series:
Start = c(1, 1)
End = c(1, 86)
Frequency = 252
 [1] 5380.35 5362.95 5366.30 5421.00 5412.85 5415.35 5386.70 5350.25 5334.60
[10] 5287.80 5315.05 5258.50 5253.75 5274.00 5225.70 5238.40 5342.10 5358.70
[19] 5363.45 5390.00 5431.00 5435.35 5577.65 5610.00 5600.05 5554.25 5691.15
[28] 5669.60 5673.90 5663.45 5649.50 5703.30 5718.80 5731.25 5787.60 5746.95
[37] 5676.00 5704.60 5652.15 5708.05 5676.05 5687.25 5648.00 5660.25 5718.70
[46] 5684.25 5717.15 5691.40 5705.30 5664.30 5665.60 5597.90 5619.70 5645.05
[55] 5697.70 5704.20 5724.40 5760.10 5738.75 5686.25 5683.70 5666.95 5631.00
[64] 5574.05 5571.40 5571.55 5614.80 5627.75 5626.60 5635.90 5727.45 5825.00
[73] 5879.85 5870.95 5889.25 5900.50 5930.90 5907.40 5908.90 5898.80 5888.00
[82] 5851.50 5879.60 5857.90 5896.80 5929.60
> summary(close.ts)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   5226    5465    5665    5624    5723    5931
> close.diff<-diff(close.ts)
> close.diff
Time Series:
Start = c(1, 2)
End = c(1, 86)
Frequency = 252
 [1] -17.40   3.35  54.70  -8.15   2.50 -28.65 -36.45 -15.65 -46.80  27.25
[11] -56.55  -4.75  20.25 -48.30  12.70 103.70  16.60   4.75  26.55  41.00
[21]   4.35 142.30  32.35  -9.95 -45.80 136.90 -21.55   4.30 -10.45 -13.95
[31]  53.80  15.50  12.45  56.35 -40.65 -70.95  28.60 -52.45  55.90 -32.00
[41]  11.20 -39.25  12.25  58.45 -34.45  32.90 -25.75  13.90 -41.00   1.30
[51] -67.70  21.80  25.35  52.65   6.50  20.20  35.70 -21.35 -52.50  -2.55
[61] -16.75 -35.95 -56.95  -2.65   0.15  43.25  12.95  -1.15   9.30  91.55
[71]  97.55  54.85  -8.90  18.30  11.25  30.40 -23.50   1.50 -10.10 -10.80
[81] -36.50  28.10 -21.70  38.90  32.80
> return<-data.diff/lag(close.ts,k=-1)
Error: object 'data.diff' not found
> return<-close.diff/lag(close.ts,k=-1)
> plot(return,main="Final Returns from 10 th to 95th day")
> plot(return,main="Final Returns from 10th to 95th day")

Assignment -2:  1-700 data is available, Predict the data from 701-850, use the GLM estimation using LOGIT Analysis for the same.
Solution:
Command Sequence
> z<-read.csv(file.choose(),header=T)
> head(z)
  age ed employ address income debtinc creddebt othdebt default
1  41  3     17      12    176     9.3    11.36    5.01       1
2  27  1     10       6     31    17.3     1.36    4.00       0
3  40  1     15      14     55     5.5     0.86    2.17       0
4  41  1     15      14    120     2.9     2.66    0.82       0
5  24  2      2       0     28    17.3     1.79    3.06       1
6  41  2      5       5     25    10.2     0.39    2.16       0
> z.data<-z[1:700,1:9]
> sapply(z.data,mean)
       age         ed     employ    address     income    debtinc   creddebt
34.8600000  1.7228571  8.3885714  8.2785714 45.6014286 10.2605714  1.5534571
   othdebt    default
 3.0582286  0.2614286
> z.data$ed<-factor(z.data$ed)
> logit.est<-glm(default~age+employ+address+income+debtinc+creddebt+othdebt,data=z.data,family="binomial")
> logitdata.est<-glm(default~age+employ+address+income+debtinc+creddebt+othdebt,data=z.data,family="binomial")
> summary(logitdata.est)

Call:
glm(formula = default ~ age + employ + address + income + debtinc +
    creddebt + othdebt, family = "binomial", data = z.data)

Deviance Residuals:
    Min       1Q   Median       3Q      Max 
-2.3659  -0.6516  -0.2882   0.2625   2.9757 

Coefficients:
             Estimate Std. Error z value Pr(>|z|)   
(Intercept) -1.376573   0.571560  -2.408   0.0160 * 
age          0.033712   0.017342   1.944   0.0519 . 
employ      -0.265086   0.031999  -8.284  < 2e-16 ***
address     -0.103960   0.023192  -4.483 7.38e-06 ***
income      -0.007566   0.008095  -0.935   0.3500   
debtinc      0.065099   0.030621   2.126   0.0335 * 
creddebt     0.628475   0.113759   5.525 3.30e-08 ***
othdebt      0.070761   0.077682   0.911   0.3623   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 804.36  on 699  degrees of freedom
Residual deviance: 552.20  on 692  degrees of freedom
AIC: 568.2

Number of Fisher Scoring iterations: 6

> confint.default(logitdata.est)
                    2.5 %      97.5 %
(Intercept) -2.4968094058 -0.25633710
age         -0.0002768342  0.06770153
employ      -0.3278025120 -0.20236959
address     -0.1494167558 -0.05850405
income      -0.0234310297  0.00829931
debtinc      0.0050836856  0.12511410
creddebt     0.4055112145  0.85143903
othdebt     -0.0814939858  0.22301505
> logitdata.eg<-with(z[701:850,1:8],data.frame(age=age,employ=employ,address=address,income=income,debtinc=debtinc,creddebt=creddebt,othdebt=othdebt,ed=factor(1:3)))
> logitdata.eg$prob<-predict(logitdata.est,newdata=logitdata.eg,type="response")
> head(logitdata.eg)
  age employ address income debtinc creddebt othdebt ed       prob
1  36     16      13     32    10.9     0.54    2.94  1 0.00865515
2  50      6      27     21    12.9     1.32    1.39  2 0.07730314
3  40      9       9     33    17.0     4.88    0.73  3 0.65161010
4  31      5       7     23     2.0     0.05    0.41  1 0.08564367
5  29      4       0     24     7.8     0.87    1.01  2 0.37406261
6  25      1       3     14     9.9     0.23    1.15  3 0.41429185




No comments:

Post a Comment