写出解决文档里的问题的R语言代码
时间: 2024-11-04 14:16:56 浏览: 5
应用回归分析(R语言)第二章 一元线性回归 部分习题代码.docx
为了完成文档中的两个任务,我们可以使用R语言编写相应的代码。以下是针对每个任务的具体实现:
### 1. 有序样品聚类(梅州市经济发展阶段划分)
```r
# 加载必要的包
library(dplyr)
library(ggplot2)
library(cluster)
# 数据输入
data <- data.frame(
年份 = 2001:2019,
x1 = c(19603, 21262, 23886, 28597, 31517, 35002, 41170, 47862, 51515, 60278, 68670, 73759, 79082, 87073, 94357, 102974, 108659, 112724, 118705),
x2 = c(3457, 6258, 7960, 8368, 10727, 11839, 13585, 16101, 10522, 21183, 13585, 14693, 15501, 16187, 17297, 18774, 18776, 19613, 21903),
x3 = c(296, 514, 605, 684, 729, 997, 3722, 10113, 2293, 3994, 5262, 7600, 9517, 12744, 13638, 13638, 13523, 15790, 17088),
x4 = c(1685, 1475, 1309, 1541, 1298, 1509, 1738, 1956, 2056, 2481, 2729, 2760, 2951, 3274, 3471, 3638, 3523, 3915, 3708),
x5 = c(8057, 7537, 9139, 18403, 20112, 21422, 24615, 28718, 20425, 23619, 27788, 31466, 34062, 38141, 42341, 46478, 54649, 57906, 59714),
x6 = c(570, 778, 988, 1991, 2637, 4011, 5115, 6287, 6667, 9422, 4277, 5146, 6243, 8141, 9144, 4647, 10546, 15790, 15971),
x7 = c(1568, 1488, 1490, 1491, 1263, 1422, 1503, 1818, 2042, 2361, 2778, 3146, 3406, 3814, 4234, 4647, 5464, 5790, 5971),
x8 = c(486.38, 488.32, 490.6, 496.89, 498.92, 500.94, 503.36, 505.28, 507.36, 514.75, 517.6, 521.35, 524.96, 538.64, 543.79, 551.4, 550.11, 548.29, 545.85),
x9 = c(208.39, 209.06, 209.43, 209.79, 211.98, 211.19, 211.72, 209.63, 209.72, 208.07, 203.45, 198.92, 193.65, 188.69, 183.28, 178.13, 174.17, 167.46, 169.02)
)
# 计算距离矩阵
dist_matrix <- dist(data[, -1], method = "euclidean")
# 进行层次聚类
hc <- hclust(dist_matrix, method = "ward.D2")
# 绘制聚类树状图
plot(hc, labels = data$年份, main = "梅州市经济发展阶段划分", sub = "", xlab = "年份", ylab = "距离")
# 划分成3个阶段
cutree_result <- cutree_result
# 输出结果
print(data)
```
### 2. 模糊聚类(内陆边远省份的社会经济发展水平)
```r
# 加载必要的包
library(e1071)
library(ggplot2)
# 数据输入
data <- data.frame(
地区 = c("内蒙古", "广西", "贵州", "云南", "西藏", "宁夏", "新疆", "甘肃", "青海"),
人均GDP = c(50683, 40763, 23422, 43553, 37164, 42703, 62293, 34563, 43673),
三产比重 = c(121.12, 34.22, 29.81, 31.12, 43.51, 37.31, 35.42, 32.81, 40.92),
人均消费 = c(1418.23, 2040.9, 1551.14, 2059.12, 1551.15, 1947.13, 2745.12, 1612.1, 2047.14),
人口增长 = c(15.83, 13.32, 14.26, 12.12, 15.95, 13.08, 12.81, 10.04, 14.48),
文盲半文盲 = c(28.98, 25.48, 7.97, 25.56, 11.44, 28.65, 42.92)
)
# 标准化数据
scaled_data <- scale(data[, -1])
# 进行模糊C均值聚类
fcm_result <- cmeans(scaled_data, centers = 3, m = 2, method = "cmeans")
# 查看聚类中心
print(fcm_result$centers)
# 将聚类结果添加到原始数据中
data$聚类 <- fcm_result$cluster
# 输出结果
print(data)
```
以上代码分别实现了对梅州市经济发展阶段的有序样品聚类和对内陆边远省份社会经济发展水平的模糊聚类分析。希望这些代码能帮助你完成实验任务。
阅读全文