Zabid Iqbal

========================================================

#Q1: Write a function rneedle(n) that simulates n drops of a needle onto paper
# and returns a data frame with n rows and 3 columns
library(readxl)
library(mlogit)
## Loading required package: Formula
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: lmtest
choicemf <- read_excel("choice_final.xlsx")

Cmf <- mlogit.data(choicemf, shape = "wide", choice = "choice",
                   varying = 5:14, sep = "_", id.var = "idx")  # mlogit data frame
## Warning: Setting row names on a tibble is deprecated.

## Warning: Setting row names on a tibble is deprecated.
mlogit1r <- mlogit(choice ~ weather+ species + nhabitats + levy,  data=Cmf,
                   panel = TRUE, rpar = c(weather = "n", species  = "n", nhabitats = "n", levy= "n"), R = 100,
                   correlation = FALSE, halton = NA, method = "bhhh", reflevel = "B")
summary(mlogit1r)
## 
## Call:
## mlogit(formula = choice ~ weather + species + nhabitats + levy, 
##     data = Cmf, reflevel = "B", rpar = c(weather = "n", species = "n", 
##         nhabitats = "n", levy = "n"), R = 100, correlation = FALSE, 
##     halton = NA, panel = TRUE, method = "bhhh")
## 
## Frequencies of alternatives:
##      B      A 
## 0.3194 0.6806 
## 
## bhhh method
## 24 iterations, 0h:1m:4s 
## g'(-H)^-1g = 8.01E-07 
## gradient close to zero 
## 
## Coefficients :
##                Estimate Std. Error z-value  Pr(>|z|)    
## A:(intercept)  2.322009   0.333233  6.9681 3.212e-12 ***
## weather        0.432026   0.178652  2.4183 0.0155948 *  
## species        0.565058   0.163611  3.4537 0.0005530 ***
## nhabitats      0.712770   0.181861  3.9193 8.881e-05 ***
## levy          -0.448064   0.090076 -4.9743 6.550e-07 ***
## sd.weather     0.596347   0.431491  1.3821 0.1669528    
## sd.species    -0.177205   0.799541 -0.2216 0.8245992    
## sd.nhabitats   1.057485   0.292268  3.6182 0.0002967 ***
## sd.levy        0.932388   0.115551  8.0690 6.661e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log-Likelihood: -926.79
## McFadden R^2:  0.20712 
## Likelihood ratio test : chisq = 484.2 (p.value = < 2.22e-16)
## 
## random coefficients
##           Min.       1st Qu.     Median       Mean   3rd Qu. Max.
## weather   -Inf  0.0297965652  0.4320264  0.4320264 0.8342563  Inf
## species   -Inf  0.4455354205  0.5650585  0.5650585 0.6845815  Inf
## nhabitats -Inf -0.0004929625  0.7127695  0.7127695 1.4260320  Inf
## levy      -Inf -1.0769500428 -0.4480638 -0.4480638 0.1808224  Inf
# References:
#  http://en.wikipedia.org/wiki/Buffon's_needle
# http://vis.supstat.com/2013/04/buffons-needle/
#http://mathworld.wolfram.com/BuffonsNeedleProblem.html