-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModels_Grayson_SoilC.Rmd
225 lines (168 loc) · 5.16 KB
/
Models_Grayson_SoilC.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
title: "Models_Grayson_Soil_C"
author: "Liying Li"
date: "2024-03-20"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
install.packages("rethinking")
```
```{r}
library(dplyr)
library(tidyr)
library(prospectr)
```
```{r}
fp1 <- "/Users/lily/Library/CloudStorage/Box-Box/Rethinking/Grayson_continuum_removal.csv"
library(readr)
spec <- read_csv(fp1)
spec
```
```{r}
# Load the necessary libraries
library(dplyr)
# Assuming your data frame is named 'df'
spec1 <- spec %>%
mutate(Class1_numeric = ifelse(Class1 == "Agriculture", 1,
ifelse(Class1 == "Riparian", 2, NA))) %>%
mutate(Class1_numeric = factor(Class1_numeric, levels = c(1, 2), labels = c("Agriculture", "Riparian")))
# View the updated data frame
print(spec1$Class1_numeric)
```
##ISE-PLS
```{r}
# Install and load necessary packages
install.packages("plspm")
library(plspm)
```
```{r}
# Load necessary libraries
library(plspm)
library(pls)
#library(rstanarm)
# Function to calculate z predictor importance for each predictor
calculate_z <- function(X, y, predictors) {
z_values <- numeric(length(predictors))
pls_model <- plsr(y ~ ., data = data.frame(X[, predictors], y), ncomp = 1) # Fit PLS model with selected predictors
for (i in seq_along(predictors)) {
predictor <- predictors[i]
regression_coefficient <- coef(pls_model)[predictor, 1] # Extract regression coefficient
predictor_std_dev <- sd(X[, predictor]) # Standard deviation of the predictor
z_values[i] <- abs(regression_coefficient * predictor_std_dev) / sum(abs(coef(pls_model))) # Calculate z
}
return(z_values)
}
# Function to fit a PLS model and return R2
fit_pls <- function(X, y) {
pls_model <- plsr(y ~ X, ncomp = 3) # Adjust ncomp as needed
return(summary(pls_model)$adjr2)
}
# Perform iterative stepwise elimination using z as the criterion
iterative_stepwise_elimination <- function(X, y) {
predictors <- names(X)
best_R2 <- 0
best_subset <- NULL
best_R2_history <- c() # Initialize an empty vector to store best R2 values
while (length(predictors) > 0) {
z_values <- calculate_z(X, y, predictors)
min_z_index <- which.min(z_values)
current_best_predictor <- predictors[min_z_index]
current_subset <- setdiff(predictors, current_best_predictor)
current_R2 <- fit_pls(X[, current_subset], y)
if (current_R2 > best_R2) {
best_R2 <- current_R2
best_subset <- current_subset
best_R2_history <- c(best_R2_history, best_R2) # Append current best R2 to history
cat("Predictor removed:", current_best_predictor, "\n")
} else {
break
}
predictors <- best_subset
}
return(list(best_subset, best_R2_history))
}
# Define predictors and response variable
X <- spec1[, -which(names(spec1) == "C")]
y <- spec1$C
# Perform iterative stepwise elimination
result <- iterative_stepwise_elimination(X, y)
# Extract selected predictors and R2 history
selected_predictors <- result[[1]]
best_R2_history <- result[[2]]
# Plot number of selected predictors against R2
plot(seq_along(selected_predictors), best_R2_history, type = "b",
xlab = "Number of Selected Predictors", ylab = "Adjusted R2",
main = "Adjusted R2 vs Number of Selected Predictors")
```
## Perform PLS get pls components for baysian models
```{r}
# Perform Partial Least Squares (PLS) analysis
pls_model <- plspm(X, y, method = "simpls")
# Summary of the PLS model
summary(pls_model)
# Plot the inner model (path coefficients)
plot(pls_model, what = "inner")
# Plot the scores of the components
plot(pls_model, what = "scores")
# Extract component scores from the PLS model
pls_scores <- predict(pls_model, what = "scores")
# Extract pls1 and pls2 from the component scores
pls1 <- pls_scores[, 1] # First component
pls2 <- pls_scores[, 2] # Second component
```
## Define the Bayesian PLS model in quap
```{r}
# Define the Bayesian PLS model
bayesian_pls_model <- quap(
alist(
mpg ~ dnorm(mu, sigma),
mu <- a + b1 * pls1 + b2 * pls2, # PLS components as predictors
pls1 ~ dnorm(0, 10),
pls2 ~ dnorm(0, 10),
a ~ dnorm(0, 10),
b1 ~ dnorm(0, 10),
b2 ~ dnorm(0, 10),
sigma ~ dnorm(0, 10)
),
data = list(
tC = spec1$C,
pls1 = as.vector(spec1[, -1]), # PLS components as predictor matrix
pls2 = as.vector(spec1[, -1])
)
)
# Summarize the model
precis(bayesian_pls_model)
# Plot the posterior distribution
plot(bayesian_pls_model)
```
##MCMC of ISE-PLS
```{r}
# Define the Bayesian PLS model using ulam()
bayesian_pls_model_ulam <- ulam(
alist(
mpg ~ dnorm(mu, sigma),
mu <- a + b1 * pls1 + b2 * pls2, # PLS components as predictors
pls1 ~ dnorm(0, 10),
pls2 ~ dnorm(0, 10),
a ~ dnorm(0, 10),
b1 ~ dnorm(0, 10),
b2 ~ dnorm(0, 10),
sigma ~ dnorm(0, 10)
),
data = list(
tC = spec1$C,
pls1 = as.vector(spec1[, -1]), # PLS components as predictor matrix
pls2 = as.vector(spec1[, -1])
),
chains = 4, # Number of chains
cores = 2 # Number of cores for parallel computing
)
# Summarize the model
precis(bayesian_pls_model_ulam)
# Plot the posterior distribution
plot(bayesian_pls_model_ulam)
```
##GA-PLS