# R语言对某地天气和温度的分析及预测

[plain] view plaincopy

1. beijing <-read.csv("BeiJing.csv",header=T,stringsAsFactors=FALSE)
2. changsha <-read.csv("Changsha.csv",header=T,stringsAsFactors=FALSE)
3. guangzhou <-read.csv("GuangZhou.csv",header=T,stringsAsFactors=FALSE)
4. shanghai <-read.csv("ShangHai.csv",header=T,stringsAsFactors=FALSE)
5. suzhou <-read.csv("SuZhou.csv",header=T,stringsAsFactors=FALSE)
6. head(suzhou,n=5)

[plain] view plaincopy

1. weatherStat <- function(x){
2. count <- numeric(0)
3. count[1:5] <- 0
4. for(i in 1:length(x[[4]])){
5. if(length(grep("雪",x[[4]][i]))>0){
6. count[1] <- count[1]+1
7. }else if(length(grep("雨",x[[4]][i]))>0){
8. count[2] <- count[2]+1
9. }else if(length(grep("晴",x[[4]][i]))>0){
10. count[3] <- count[3]+1
11. }else if((length(grep("阴",x[[4]][i]))>0)||(length(grep("多云",x[[4]][i]))>0)||(length(grep("雾",x[[4]][i]))>0)||(length(grep("霾",x[[4]][i]))>0)||(length(grep("浮尘",x[[4]][i]))>0)){
12. count[4] <- count[4]+1
13. }else{
14. count[5] <- count[5]+1
15. print(x[[4]][i])
16. }
17. }
18. count
19. }
20. statAll<-list(beijing=numeric(0),suzhou=numeric(0),shanghai=numeric(0),changsha=numeric(0),guangzhou=numeric(0))
21. statAll\$suzhou<- weatherStat(suzhou)
22. statAll\$beijing<- weatherStat(beijing)
23. statAll\$shanghai<- weatherStat(shanghai)
24. statAll\$changsha<- weatherStat(changsha)
25. statAll\$guangzhou<- weatherStat(guangzhou)
26. statAll<- as.data.frame(statAll)
27. statAll<- statAll[-5,]#第五行，是统计除这四种天气外还是否有其他情况，五个城市都为0，所以删除这一行，前面雾、霾、浮尘都是在这个类别下发现然后整理到阴下面的
28. rownames(statAll)<- c("雪","雨","晴","阴")
29. colnames(statAll)<- c("北京","苏州","上海","长沙","广州")
30. statAll
31. statAll<- as.matrix(statAll)
32. barplot(statAll,legend=TRUE,col=c("snow3","lavender","khaki1","lemonchiffon"))

[plain] view plaincopy

1. #2011年1:354,2012年355:720,2013年721:1085,2014年1086:1450,2015年1451:1542
2. statSuzhou<-list(one=numeric(0),two=numeric(0),three=numeric(0),four=numeric(0),five=numeric(0))
3. statSuzhou\$one<- weatherStat(suzhou[1:354,])
4. statSuzhou\$two<- weatherStat(suzhou[355:720,])
5. statSuzhou\$three<- weatherStat(suzhou[721:1085,])
6. statSuzhou\$four<- weatherStat(suzhou[1086:1450,])
7. statSuzhou\$five<- weatherStat(suzhou[1451:1542,])
8. statSuzhou<- as.data.frame(statSuzhou)
9. statSuzhou<- statSuzhou[-5,]#第五行，是统计除这四种天气外还是否有其他情况，五个城市都为0，所以删除这一行
10. rownames(statSuzhou)<- c("雪","雨","晴","阴")
11. colnames(statSuzhou)<-c("2011","2012","2013","2014","2015")
12. statSuzhou
13. statSuzhou<- as.matrix(statSuzhou)
14. barplot(statSuzhou,legend=TRUE,col=c("snow3","lavender","khaki1","lemonchiffon"))

？查了一下正常的梅雨季节应该是六月到七月，我最前面的梅雨季节的说法不恰当，这里就不改了。

[plain] view plaincopy

1. statMonthly<- data.frame()
2. statMonthly[1:5,1]
3. count<- 1
4. for(i in2011:2015){
5. if(i != 2015){
6. for (j in 1:12) {
7. temp <-nrow(suzhou[grep(paste(i,"-",j,"-",sep=""),suzhou\$date),])
8. statMonthly[1:5,((i-2011)*12+j)]<- weatherStat(suzhou[count:(count+temp-1),])
9. print(weatherStat(suzhou[count:(count+temp-1),]))
10. print(c(((i-2011)*12+j)))
11. count <- count + temp
12. }
13. }else{
14. for (j in 1:3) {
15. temp <-nrow(suzhou[grep(paste(i,"-",j,"-",sep=""),suzhou\$date),])
16. statMonthly[1:5,((i-2011)*12+j)]<- weatherStat(suzhou[count:(count+temp-1),])
17. print(weatherStat(suzhou[count:(count+temp-1),]))
18. print(c(((i-2011)*12+j)))
19. count <- count + temp
20. }
21. }
22. }
23. statMonthly<- statMonthly[-5,]
24. rownames(statMonthly)<- c("雪","雨","晴","阴")
25. colnames(statMonthly)<-c(paste(rep(2011,12),1:12,sep="-"),paste(rep(2012,12),1:12,sep="-"),paste(rep(2013,12),1:12,sep="-"),paste(rep(2014,12),1:12,sep="-"),paste(rep(2015,3),1:3,sep="-"))
26. statMonthly<- as.matrix(statMonthly)
27. barplot(statMonthly,legend=TRUE,col=c("snow3","lavender","khaki1","lemonchiffon"))
28. barplot(statMonthly,col=c("snow3","lavender","khaki1","lemonchiffon"),cex.names=.6)
29. lines(statMonthly[2,],type="l",col="red")
30. statMonthly

[plain] view plaincopy

1. statMonthly2<- data.frame()
2. temp<- 1:51
3. for(i in1:12){
4. if(i==12){
5. for(j in 1:4){
6. statMonthly2[j,12]<- sum(statMonthly[j,temp%%12==0&temp<49])
7. }
8. }else{
9. for(jin 1:4){
10. statMonthly2[j,i]<- sum(statMonthly[j,temp%%12==i&temp<49])
11. }
12. }
13. }
14. rownames(statMonthly2)<- c("雪","雨","晴","阴")
15. colnames(statMonthly2)<- 1:12
16. statMonthly2<- as.matrix(statMonthly2)
17. barplot(statMonthly2,col=c("snow3","lavender","khaki1","lemonchiffon"),cex.names=.8)

[plain] view plaincopy

1. plot(suzhou\$highestTemp,type="l",col="red",main="苏州2011-2015年气温图",xlab="时间轴",ylab="温度℃")
2. lines(suzhou\$lowestTemp,type="l",col="blue")
3. legend("topright",c("最高气温","最低气温"),col=c("red","blue"),lty=1)

[plain] view plaincopy

1. avgTemper <-numeric(0)#月平均气温
2. diffTemper <-numeric(0)#月最高温差
3. length(avgTemper)<- 48
4. length(diffTemper)<- 48
5. for(i in2011:2014){
6. for(j in 1:12){
7. print((i-2011)*12+j)
8. avgTemper[(i-2011)*12+j]<-mean(c(suzhou\$highestTemp[grep(paste(i,"-",j,"-",sep=""),suzhou\$date)],suzhou\$lowestTemp[grep(paste(i,"-",j,"-",sep=""),suzhou\$date)]),na.rm=TRUE)
9. diffTemper[(i-2011)*12+j]<- max(suzhou\$highestTemp[grep(paste(i,"-",j,"-",sep=""),suzhou\$date)]-suzhou\$lowestTemp[grep(paste(i,"-",j,"-",sep=""),suzhou\$date)],na.rm=TRUE)
10. }
11. }
12. avgTemperTS<- ts(avgTemper,frequency=12,start=c(2011,1))
13. plot.ts(avgTemperTS,main="苏州2011-2014年月平均气温图",xlab="时间",ylab="月平均温度℃")

[plain] view plaincopy

1. plot.ts(avgTemperTS[25:36],col="red",main="苏州2011-2014年月平均气温图",xlab="月份",ylab="月平均温度℃")
2. lines(avgTemperTS[1:12],col="black")
3. lines(avgTemperTS[13:24],col="blue")
4. lines(avgTemperTS[37:48],col="green")
5. legend("topright",c("2011","2012","2013","2014"),col=c("black","blue","red","green"),lty=1)

[plain] view plaincopy

1. which.max(suzhou[,2])
2. suzhou[940,]

[plain] view plaincopy

1. diffTemperTS <-ts(diffTemper,frequency=12,start=c(2011,1))
2. plot.ts(diffTemperTS,main="苏州2011-2014年月最大温差图",xlab="时间",ylab="月最大温差℃")
3. plot.ts(diffTemperTS[25:36],col="red",ylim=c(8,23),main="苏州2011-2014年月最大温差图",xlab="月份",ylab="月最大温差℃")
4. lines(diffTemperTS[1:12],col="black")
5. lines(diffTemperTS[13:24],col="blue")
6. lines(diffTemperTS[37:48],col="green")
7. legend("topright",c("2011","2012","2013","2014"),col=c("black","blue","red","green"),lty=1)

[plain] view plaincopy

1. which.max(suzhou\$highestTemp[grep(paste(2013,"-",3,"-",sep=""),suzhou\$date)]-suzhou\$lowestTemp[grep(paste(2013,"-",3,"-",sep=""),suzhou\$date)])
2. suzhou[grep("2013-3-9",suzhou\$date),]

[plain] view plaincopy

1. highestTS <-ts(suzhou\$highestTemp[1:(360*4)],frequency=360,start=c(2011,1,1))
2. plot.ts(highestTS,col="red")
3. lowestTS <-ts(suzhou\$lowestTemp[1:(360*4)],frequency=360,start=c(2011,1,1))
4. lines(lowestTS,col="blue")

[plain] view plaincopy

1. highestForecasts<- HoltWinters(highestTS)
2. highestForecasts
3. lowestForecasts<- HoltWinters(lowestTS)
4. lowestForecasts

[plain] view plaincopy

1. library("forecast")
2. highestForecast2<- forecast.HoltWinters(highestForecasts,h=150)
3. lowestForecast2<- forecast.HoltWinters(lowestForecasts,h=150)
4. plot(highestForecast2\$mean,ylim=c(-4,32),col="red",main="苏州2015年最高最低气温预测时序图",xlab="时间",ylab="温度℃")
5. lines(lowestForecast2\$mean,col="blue")
6. legend("topright",c("最高气温","最低气温"),col=c("red","blue"),lty=1)

[plain] view plaincopy

1. Box.test(highestForecast2\$residuals,lag=20,type="Ljung-Box")
2. Box.test(lowestForecast2\$residuals,lag=20,type="Ljung-Box")

[plain] view plaincopy

1. highestFactTS <- ts(suzhou\$highestTemp[1451:1542],frequency=360,start=c(2015,1))
2. lowestFactTS <- ts(suzhou\$lowestTemp[1451:1542],frequency=360,start=c(2015,1))
3. plot(highestForecast2\$mean,col="red",main="苏州2015年最高气温预测观察值时序图",xlab="时间",ylab="温度℃")
4. lines(highestFactTS,col="orange")
5. legend("topleft",c("实际值","预测值"),col=c("orange","red"),lty=1)

[plain] view plaincopy

1. plot(lowestForecast2\$mean,col="blue",main="苏州2015年最低气温预测观察值时序图",xlab="时间",ylab="温度℃")
2. lines(lowestFactTS,col="green")
3. legend("topleft",c("实际值","预测值"),col=c("green","blue"),lty=1)

[plain] view plaincopy

1. predictTemp <-list(date="",highestTemp="",lowestTemp="")
2. predictTemp\$date <-c(paste(rep(2015,50),c(rep(4,20),rep(5,30)),c(11:30,1:20),sep="-"))
3. predictTemp\$highestTemp <-round(highestForecast2\$mean[101:150])
4. predictTemp\$lowestTemp <-round(lowestForecast2\$mean[101:150])
5. predictTemp <-as.data.frame(predictTemp)

822 篇文章234 人订阅

0 条评论

## 相关文章

### 麦克阿瑟天才奖得主解码计算机视觉“原罪”：AI 如何认识人类世界

【新智元导读】麦克阿瑟“天才奖”获得者Trevor Paglen训练AI算法，他的展览项目“看不见的图像的研究”（A Study of Invisible Im...

37770

### 薛定谔的滚与深度学习中的物理

【新智元导读】作者从薛定谔的“滚”讲到世界的量子性、神经网络的最大似然等等，用颇具趣味的方式呈现了深度学习中无处不在的物理本质。 最近朋友圈里有大神分享薛定谔的...

35250

18630

12710

13020

46960

35180

304100

### 安恒信息两篇核心AI异常检测论文入选IEEE DSC国际会议

6月18日-21日，“第三届IEEE网络空间数据科学国际会议”在广州召开。业界代表及专家齐聚一堂，并就网络空间数据科学的科研和前沿发展方向进行交流。而安恒信息的...

22740

13540