问题1:
问题2:给定数据,请完成以下任务,请给出code 和输出结果。
(1) 请读入数据,使用软件分别给出 price, marketshare,和brand的缺失值数量。请按照每一个brand, 将数据按照先marjetshare 后price 进行从高到低排序
(2) 请按照brand 的种类,对price和marketshare 求均值。
(3)请按照brand 的种类,对price和marketshare 画散点图。
(4) 请按照价格的均值,产生新的变量price_new, 低于均值为“低价格”,高于均值为“高价格”。同样对市场份额也是,产生变量marketshare_new, 数值为“低市场份额”和“高市场份额”
(5) 请估计模型,marketshare为Y,price为X.
(6) 请画出(5)的拟合直线。
(7)请随机产生若干直线,验证(5)的结果是最优的
(8) 请估计模型,marketshare为Y,price和brand 为X.
######### 1(1) ############
get.root<-function(a,b,c){
if(sign(b*b-4*a*c)==-1)
{print("无解")
return(c(NA,NA))
} else
return(c((-b+sqrt(b*b-4*a*c))/(2*a),(-b-sqrt(b*b-4*a*c))/(2*a)))
}
get.root(1,-4,4)
######### 1(2) #############
get.prob<-function(n){
a=runif(n,min=1,max=5)
b=rnorm(n,mean=3,sd=sqrt(10))
c=rexp(n,rate=1)
k=0
for (i in 1:n) {
if(sign(b[i]*b[i]-4*a[i]*c[i])==1|0)
{k=k+1}
}
return(k/n)
}
get.prob(100000)
########## 2(1) ############
library(readxl)
library(tidyverse)
data<-read_xlsx("data for HW2.xlsx")
sum(is.na(data$price))
sum(is.na(data$marketshare))
sum(is.na(data$brand))
data1=filter(data,!is.na(data[1]))
data1=filter(data1,!is.na(data1[2]))
data1=filter(data1,!is.na(data1[3]))
data1=arrange(data1,desc(marketshare,price))
########### 2(2) ############
pricebars <- data1 %>%
group_by(brand) %>%
summarize(pricebar=mean(price))
pricebars
marketsharebars <- data1 %>%
group_by(brand) %>%
summarise(marketsharebar=mean(marketshare))
marketsharebars
######### 2(3) ################
ggplot(data=data1)+
geom_point(mapping = aes(x=price,y=marketshare))+
facet_wrap(~brand,nrow=2)
######## 2(4) ##########
price=data1$price
pricebar=mean(price)
price_new=ifelse(price>pricebar,"高价格","低价格")
marketshare=data1$marketshare
marketsharebar=mean(marketshare)
marketshare_new=ifelse(marketshare>marketsharebar,"高市场份额",
"低市场份额")
data1=mutate(data1,price_new,marketshare_new)
######### 2(5) #########
m1=lm(marketshare~price,data=data1)
m1
summary(m1)
######### 2(6) #########
ggplot(data=data1)+
geom_point(aes(x=price,y=marketshare))+
geom_abline(data= m1,col= "blue")
######### 2(7) ##########
b0=runif(20000,-5,5)
b1=runif(20000,-5,5)
d<-NA
sum<-NA
n<-1
while(n<=20000){
for(i in 1:24){
d[i]<-(marketshare[i]-b0[n]-b1[n]*price[i])^2}
sum[n]<-sum(d)
n<-n+1
}
resi=m1$residuals
resi2=sum(resi^2)
check=sum(as.numeric(sum<resi2))
######## 2(8) #########
m2=lm(marketshare~price+brand,data=data1)
m2
summary(m2)
问题1:
n_Sim <- 50000
sim_meet <- tibble(
A =runif(n_Sim, min = 0, max = 60),
B =runif(n_Sim, min = 0, max = 60)
) %>%
mutate(result = ifelse(abs(A - B) <= 10,
"They meet","They do not"))
p_meet <- sim_meet %>% count(result)%>%
arrange(n) %>%
mutate(percent = n / n_Sim)
p_meet
ggplot(data = sim_meet, aes(x = A, y = B,color = result)) +
geom_point()
## 最后一问就是学生不断修改min = 10, max = 50
问题2:
(1) 从flights数据表中挑选出以下变量:(year, month, day, hour, origin, dep_delay, distance, carrier),将生产的新表保存为 flight1。
library(tidyverse)
library(nycflights13)
flight1<-select(flights,
year, month, day, hour, origin, dep_delay, distance, carrier)
(2) 从weather数据表中挑选出以下变量:(year, month, day, hour, origin, humid, wind_speed),将生产的新表保存为 weather1。
weather1<-select(weather,
year, month, day, hour, origin, humid, wind_speed)
(3) 将flight1表和weather1表根据共同变量进行内连接,随机抽取100000行数据,将生产的结果保存为flight_weather。(提示:sample_n()函数,不用重复抽取)
flight_weather <- inner_join(flight1, weather1) %>%
sample_n(100000)
(4) 从flight_weather 表中对三个出发机场按照平均出发延误时间排降序,并将结果保留在longest_delay 表中。把结果展示出来。
longest_delay<- flight_weather %>% group_by(origin) %>%
summarise(ave_delay = mean(dep_delay, na.rm = TRUE)) %>%
arrange(desc(ave_delay))
(5) 根据出发地(origin) 在同一个图中画出风速 wind_speed(x轴)和出发延误时间dep_delay(y轴)的平滑曲线图
ggplot(data =
flight_weather)+geom_smooth(mapping = aes(x = wind_speed, y = dep_delay,
linetype = origin))
(6)根据不同出发地(origin) 在平行的3个图中画出风速wind_speed(x轴)和出发延误时间dep_delay(y轴)的散点图。
ggplot(data
= flight_weather) + geom_point(mapping = aes(x = wind_speed, y = dep_delay)) + facet_wrap(~origin,nrow= 1)
(7) 根据flight_weather表,画出每个月航班数的直方分布图,x轴为月份,y轴是每个月份航班数所占的比例。
ggplot(data=flight_weather)+geom_bar(mapping
= aes(month, y=..prop.., group = 1) )
(8) 根据flight_weather表,画出每个月航班距离的boxplot图,x轴为月份,y轴为航行距离, 根据的航行距离的中位数从低到高对x轴的月份进行重新排序。
ggplot(data=flight_weather)+geom_boxplot(mapping = aes(x=reorder(month,
distance, FUN=median), y=distance))
问题3:
######(1) #######
(H <- function(p) -sum(p*log(p)))
######(2) #######
(DKL <- function(p,q) sum( p*(log(p)-log(q)) ))
###### (3)#######
IB <- list()
IB[[1]] <- c( 0.2 , 0.2 , 0.2 , 0.2 , 0.2 )
IB[[2]] <- c( 0.8 , 0.1 , 0.05 , 0.025 , 0.025 )
IB[[3]] <- c( 0.05 , 0.15 , 0.7 , 0.05 , 0.05 )
purrr::map_dbl( IB , H )
[1] 1.6094379 0.7430039 0.9836003
###### (4)#######
Dm <- matrix( NA , nrow=3 , ncol=3 )
for ( i in 1:3 ) {
for ( j in 1:3) {
Dm[i,j]<- DKL( IB[[j]] , IB[[i]] )
}
}
Dm