CS计算机代考程序代写 # BS1033 Lecture 4 Analysis
# BS1033 Lecture 4 Analysis
# Author: Chris Hansman
# Email: chansman@imperial.ac.uk
# Date : 28/01/19
# Installing Packages
#install.packages(“tidyverse”)
# Loading Libraries
library(tidyverse)
#————————————————–#
# Functions in r
#————————————————–#
# A function that adds two numbers
addition <- function(x,y){
x+y
}
#Evaluating Our Addition Function
addition(2,2)
# A function that squares a number minus 1
xsquared <- function(x){
(x-1)^2
}
#Evaluating our Square Function
xsquared(1)
# Specify range of x-axis
ggplot(data.frame(x = c(0,3) ), aes(x=x)) +
stat_function(fun = xsquared)
#Minimizing our square function
z<-optim(par=1, fn=xsquared)
z$par
#--------------------------------------------------#
# Simulating Probit Data
#--------------------------------------------------#
#Setting Seed
set.seed(050187)
# Choosing parameters
beta_0 <- 0.2
beta_1 <- 0.5
n <- 10000
# Simulating Data
data <- tibble(
#Simulating x and v
x_i = rnorm(n),
v_i = rnorm(n),
#Generating latent index
y_i_star=beta_0+beta_1*x_i+v_i,
#Generating observed binary variable
y_i = y_i_star>0
)
#Converting Data to Vector Form
x_i <- data.matrix(data$x_i)
y_i <- data.matrix(data$y_i)
#--------------------------------------------------#
# Writing a Likelihood Function
#--------------------------------------------------#
loglik <- function(beta){
#Log(Phi(Xbeta))
l_1 <- log(pnorm(beta[1]+beta[2]*x_i))
#Log(1-Phi(Xbeta))
l_2 <- log(1-pnorm(beta[1]+beta[2]*x_i))
#Summing Everything
-sum(y_i*l_1+(1-y_i)*l_2)
}
loglik(c(1,1))
#Minimizing our likelihood function
probit_ll<-optim(par=c(1,1), fn=loglik)
#Value of Likelihood at minimum
probit_ll$val
#Value of Parameters at Minimum
probit_ll$par
# The Easy Way
probit_glm <- glm(y_i ~ x_i, family = binomial(link = "probit"), data = data)
summary(probit_glm)
logLik(probit_glm)
#Plotting Likelihood
#Creating Univariate Function
loglik_b1 <- function(x){
(loglik(c(0.2,x)))
}
#Vectorizing Univariate Function
loglik_b1<-Vectorize(loglik_b1, vectorize.args="x")
#Vectorizing Univariate Function
#Plotting Likelihood
#Creating Univariate Function
loglik_b2 <- function(x){
(-loglik(c(0.2,x)))
}
#Vectorizing Univariate Function
loglik_b2<-Vectorize(loglik_b2, vectorize.args="x")
#Plotting Likelihood
ggplot(data.frame(x = c(0.25,0.75) ), aes(x=x)) +
stat_function(fun = loglik_b1) +
ylab("Log-Likelihood") +
xlab(expression(beta[1])) +
theme_classic()
ggsave("Log_Likelihood.pdf")
#Plotting Likelihood
ggplot(data.frame(x = c(0.25,0.75) ), aes(x=x)) +
stat_function(fun = loglik_b2) +
ylab("Log-Likelihood") +
xlab(expression(beta[1])) +
theme_classic()
ggsave("Log_Likelihood2.pdf")
#Plotting Likelihood
ggplot(data = data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = pnorm, n = 101, args = list(mean = 0, sd = 1)) + ylab("Probability") +
theme_classic()
ggsave("cdf.pdf")
#Plotting Likelihood
ggplot(data = data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = plogis, n = 101) + ylab("Probability") +
theme_classic()
ggsave("cdf2.pdf")