> head(z)
Date Open High Low Close Shares.Traded Turnover..Rs..Cr.
1 02-Jul-2012 5283.85 5302.15 5263.35 5278.60 126161441 4991.57
2 03-Jul-2012 5298.85 5317.00 5265.95 5287.95 133117055 5161.82
3 04-Jul-2012 5310.40 5317.65 5273.30 5302.55 155995887 5750.10
4 05-Jul-2012 5297.05 5333.65 5288.85 5327.30 118915392 4709.79
5 06-Jul-2012 5324.70 5327.20 5287.75 5316.95 113300726 4760.51
6 09-Jul-2012 5283.70 5300.60 5257.75 5275.15 101169926 4189.25
> close<-z$Close[10:95]
> close.ts<-ts(close,deltat=//252)
Error: unexpected '/' in "close.ts<-ts(close,deltat=/"
> close.ts<-ts(close,deltat=1/252)
> close.ts
Time Series:
Start = c(1, 1)
End = c(1, 86)
Frequency = 252
[1] 5227.25 5197.25 5192.85 5216.30 5242.70 5205.10 5117.95 5128.20 5109.60
[10] 5043.00 5099.85 5199.80 5229.00 5240.50 5227.75 5215.70 5282.55 5336.70
[19] 5338.00 5322.95 5320.40 5347.90 5380.35 5362.95 5366.30 5421.00 5412.85
[28] 5415.35 5386.70 5350.25 5334.60 5287.80 5315.05 5258.50 5253.75 5274.00
[37] 5225.70 5238.40 5342.10 5358.70 5363.45 5390.00 5431.00 5435.35 5577.65
[46] 5610.00 5600.05 5554.25 5691.15 5669.60 5673.90 5663.45 5649.50 5703.30
[55] 5718.80 5731.25 5787.60 5746.95 5676.00 5704.60 5652.15 5708.05 5676.05
[64] 5687.25 5648.00 5660.25 5718.70 5684.25 5717.15 5691.40 5705.30 5664.30
[73] 5665.60 5597.90 5619.70 5645.05 5697.70 5704.20 5724.40 5760.10 5738.75
[82] 5686.25 5683.70 5666.95 5631.00 5574.05
> summary(close.ts)
Min. 1st Qu. Median Mean 3rd Qu. Max.
5043 5284 5433 5475 5676 5788
> z.diff<-diff(close.ts)
> z.diff
Time Series:
Start = c(1, 2)
End = c(1, 86)
Frequency = 252
[1] -30.00 -4.40 23.45 26.40 -37.60 -87.15 10.25 -18.60 -66.60 56.85
[11] 99.95 29.20 11.50 -12.75 -12.05 66.85 54.15 1.30 -15.05 -2.55
[21] 27.50 32.45 -17.40 3.35 54.70 -8.15 2.50 -28.65 -36.45 -15.65
[31] -46.80 27.25 -56.55 -4.75 20.25 -48.30 12.70 103.70 16.60 4.75
[41] 26.55 41.00 4.35 142.30 32.35 -9.95 -45.80 136.90 -21.55 4.30
[51] -10.45 -13.95 53.80 15.50 12.45 56.35 -40.65 -70.95 28.60 -52.45
[61] 55.90 -32.00 11.20 -39.25 12.25 58.45 -34.45 32.90 -25.75 13.90
[71] -41.00 1.30 -67.70 21.80 25.35 52.65 6.50 20.20 35.70 -21.35
[81] -52.50 -2.55 -16.75 -35.95 -56.95
> returns<-z.diff/lag(close.ts,k=-1)
> returns
Time Series:
Start = c(1, 2)
End = c(1, 86)
Frequency = 252
[1] -0.0057391554 -0.0008466016 0.0045158246 0.0050610586 -0.0071718771
[6] -0.0167431942 0.0020027550 -0.0036270036 -0.0130342884 0.0112730518
[11] 0.0195986156 0.0056156006 0.0021992733 -0.0024329740 -0.0023050069
[16] 0.0128170715 0.0102507312 0.0002435962 -0.0028194080 -0.0004790577
[21] 0.0051687843 0.0060678023 -0.0032339904 0.0006246562 0.0101932430
[26] -0.0015034127 0.0004618639 -0.0052905168 -0.0067666660 -0.0029250970
[31] -0.0087729164 0.0051533719 -0.0106395989 -0.0009032994 0.0038543897
[36] -0.0091581342 0.0024302964 0.0197961210 0.0031073922 0.0008864090
[41] 0.0049501720 0.0076066790 0.0008009575 0.0261804668 0.0057999337
[46] -0.0017736185 -0.0081784984 0.0246477922 -0.0037865809 0.0007584309
[51] -0.0018417667 -0.0024631629 0.0095229666 0.0027177248 0.0021770301
[56] 0.0098320611 -0.0070236367 -0.0123456790 0.0050387597 -0.0091943344
[61] 0.0098900418 -0.0056061177 0.0019732032 -0.0069014023 0.0021689093
[66] 0.0103263990 -0.0060240964 0.0057879228 -0.0045039924 0.0024422813
[71] -0.0071863005 0.0002295076 -0.0119493081 0.0038943175 0.0045109170
[76] 0.0093267553 0.0011408112 0.0035412503 0.0062364615 -0.0037065329
[81] -0.0091483337 -0.0004484502 -0.0029470239 -0.0063438005 -0.0101136565
> plot(returns)
>
Assignment 2: Logit Analysis and Predicting data from 701 to 850
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
> data<-z[1:700,1:9]
> sapply(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
> data$ed<-factor(data$ed)
> logit.est<-glm(default~age+employ+address+income+debtinc+creddebt+othdebt,data=data,family="binomial")
> summary(logit.est)
Call:
glm(formula = default ~ age + employ + address + income + debtinc +
creddebt + othdebt, family = "binomial", data = 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(logit.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
> logit.eg2<-with(z[701:850,1:8],data.frame(age=mean(age),employ=mean(employ),address=mean(address),income=mean(income),debinc=mean(debtinc),creddebt=mean(creddebt),othdebt=mean(othdebt),ed=factor(1:3)))
> logit.eg2$prob<-predict(logit.est,newdata=logit.eg2,type="response")
Error in eval(expr, envir, enclos) : object 'debtinc' not found
> logit.eg2<-with(z[701:850,1:8],data.frame(age=mean(age),employ=mean(employ),address=mean(address),income=mean(income),debtinc=mean(debtinc),creddebt=mean(creddebt),othdebt=mean(othdebt),ed=factor(1:3)))
> logit.eg2$prob<-predict(logit.est,newdata=logit.eg2,type="response")
> head(logit.eg2)
age employ address income debtinc creddebt othdebt ed prob
1 35.82 9.393333 8.806667 51.68667 9.756667 1.6852 3.174933 1 0.1143839
2 35.82 9.393333 8.806667 51.68667 9.756667 1.6852 3.174933 2 0.1143839
3 35.82 9.393333 8.806667 51.68667 9.756667 1.6852 3.174933 3 0.1143839
>
No comments:
Post a Comment