A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```
herbivore_data<-read.csv("C:/Users/Dr Invasive/Desktop/herbivory dat/herbivory data 2.csv")
herbivore_data$PaperID <-as.factor(herbivore_data$PaperID)
dput(head(herbivore_data, 20))
species_list=unique(herbivore_data $Species)
species_list =data.frame(species_list)
#To pull the phylogeny
#https://cran.r-project.org/web/packages/rotl/vignettes/data_mashups.html
species_list$species<-as.character(species_list$species)
sapply(species_list,class)
taxon_search <- tnrs_match_names(names =species_list$species, context_name = "All life")
knitr::kable(taxon_search)
species_list $ott_name <- unique_name(taxon_search)
species_list $ott_id <- taxon_search$ott_id
hits <- lapply(species_list $ott_id, studies_find_trees, property = "ot:ottId", detailed = FALSE)
sapply(hits, function(x) sum(x[["n_matched_trees"]]))
ott_in_tree <- ott_id(taxon_search)[is_in_tree(ott_id(taxon_search))]
tr <- tol_induced_subtree(ott_ids = ott_in_tree)
plot(tr)
tr$tip.label <- strip_ott_ids(tr$tip.label, remove_underscores = TRUE)
tr$tip.label %in% species_list $ott_name
## computes branch lengths (default method is "Grafen", but I don't even see alternatives in the help vignette so I did not specify anything)
tree2<-compute.brlen(tr)
#http://www.phytools.org/Cordoba2017/ex/2/Intro-to-phylogenies.html
tree2
plot(tree2,type="fan")
plot(tree2,no.margin=TRUE)
plot(tree2,no.margin=TRUE)
plot(tree2)
write.nexus(tree2, file = "your_tree.nexus")
tree2$edge.length
specieslist_tree2<-tree2 $tip
##Check if the species list from the dataset and the sigma file match each other
Species=unique(herbivore_data $Species)
Species =data.frame(Species)
Species <- Species[order(Species$Species),]
specieslist_tree2 =data.frame(specieslist_tree2)
specieslist_tree2 <- specieslist_tree2[order(specieslist_tree2$specieslist_tree2),]
all.equal(Species, specieslist_tree2)
identical(Species, specieslist_tree2)
# Elements in Species that are not in specieslist_tree2
diff1 <- setdiff(Species, specieslist_tree2)
# Elements in specieslist_tree2 that are not in Species
diff2 <- setdiff(specieslist_tree2, Species)
# Check if the vectors have the same elements (ignoring order)
are_equal <- setequal(Species, specieslist_tree2)
# Check the class of the vectors
class(Species)
class(specieslist_tree2)
# Check the length of the vectors
length(Species)
length(specieslist_tree2)
# Elements in Species that are not in specieslist_tree2
diff1 <- setdiff(Species, specieslist_tree2)
# Elements in specieslist_tree2 that are not in Species
diff2 <- setdiff(specieslist_tree2, Species)
# Check if the vectors have the same elements (ignoring order)
are_equal <- setequal(Species, specieslist_tree2)
##Check if the species list from the dataset and the sigma file match each other
Species=unique(herbivore_data $Species)
Species =data.frame(Species)
Species <- Species[order(Species$Species),]
specieslist_tree2 =data.frame(specieslist_tree2)
specieslist_tree2 <- specieslist_tree2[order(specieslist_tree2$specieslist_tree2),]
all.equal(Species, specieslist_tree2)
identical(Species, specieslist_tree2)
# Elements in Species that are different from specieslist_tree2
different_elements <- Species[Species != specieslist_tree2]
# Elements in specieslist_tree2 that are different from Species
different_elements2 <- specieslist_tree2[specieslist_tree2 != Species]
# Print the differing elements
print(different_elements)
print(different_elements2)
# Remove leading/trailing spaces and convert to lowercase
Species_cleaned <- tolower(trimws(Species))
specieslist_tree2_cleaned <- tolower(trimws(specieslist_tree2))
# Check if the cleaned vectors are identical
are_equal_cleaned <- identical(Species_cleaned, specieslist_tree2_cleaned)
# Check if the cleaned vectors are identical
are_equal_cleaned <- identical(Species_cleaned, specieslist_tree2_cleaned)
# Print the result
print(are_equal_cleaned)
# Identify the differing elements
differing_elements <- Species_cleaned[Species_cleaned != specieslist_tree2_cleaned]
# Print the differing elements
print(differing_elements)
# Check if the cleaned vectors are identical, ignoring case
are_equal_ignore_case <- identical(tolower(Species), tolower(specieslist_tree2))
# Print the result
print(are_equal_ignore_case)
# Find elements in Species_cleaned that are not in specieslist_tree2_cleaned
diff1 <- setdiff(Species_cleaned, specieslist_tree2_cleaned)
# Find elements in specieslist_tree2_cleaned that are not in Species_cleaned
diff2 <- setdiff(specieslist_tree2_cleaned, Species_cleaned)
# Print the differing elements
print(diff1)
print(diff2)
# Remove additional text in parentheses and leading/trailing spaces
Species_cleaned <- gsub("\\s*\\(.*?\\)\\s*", "", Species_cleaned)
# Check if the cleaned vectors are identical
are_equal_cleaned <- identical(Species_cleaned, specieslist_tree2_cleaned)
# Print the result
print(are_equal_cleaned)
# Identify the differing elements
differing_elements <- Species_cleaned[Species_cleaned != specieslist_tree2_cleaned]
# Print the differing elements
print(differing_elements)
your_data <- read.csv("C:/Users/Dr Invasive/Desktop/herbivory dat/herbivory data 2.csv")
Species <- read.csv("C:/Users/Dr Invasive/Desktop/herbivory dat/herbivory data 2.csv")
specieslist_tree2 <- read.csv("C:/Users/Dr Invasive/Desktop/herbivory dat/herbivory data 2.csv")
class(Species)
class(specieslist_tree2)
# Check the number of rows in both data frames
nrow(Species)
nrow(specieslist_tree2)
# Check the column names of both data frames
colnames(Species)
colnames(specieslist_tree2)
file_path <- file.path("C:/Users/Dr Invasive/Desktop/herbivory dat/herbivory data 2.csv")
file_path <- "C:/Users/Dr Invasive/Desktop/herbivory dat/herbivory data 2.csv"
if (!file.exists(file_path)) {
cat("The file does not exist at the specified path.\n")
}
# Check the current working directory
getwd()
# Set the working directory to the desired location
setwd("C:/Users/Dr Invasive/Desktop/herbivory dat/")
# generate the variance-covariance matrix and write out the file
sigma<-vcv.phylo(tree2, cor=TRUE)
write.table(sigma, file="C:/Users/Dr Invasive/Desktop/herbivory dat/herbivory data 2.csv")
# Generate the variance-covariance matrix
sigma <- vcv.phylo(tree2, cor=TRUE)
# Print the variance-covariance matrix
print(sigma)
colnames(herbivore_data)
plot(herbivore_data$g_overallacross, herbivore_data$g_overallacross)
# Create the scatterplot
plot(herbivore_data$g_overallacross, herbivore_data$go_varacross,
xlab = "g_overallacross", ylab = "go_varacross", main = "Scatterplot of g_overallacross vs. go_varacross")
# Add labels for data points
text(herbivore_data$g_overallacross, herbivore_data$go_varacross, labels = herbivore_data$Species, pos = 3, cex = 0.7)
plot(herbivore_data$g_overallacross~herbivore_data$go_varacross)
#Excluding treatments that are not relevant to contemporary climate change
herbivore_data$treatment <- factor(herbivore_data$treatment)
str(herbivore_data)
herb_data<-herbivore_data[!(herbivore_data$treatment %in% c("CO2_flooding", "CO2_light", "CO2_O3","flooding", "Increased_precipitation", "light", "O3", "Temperature_increased_precipitation")), ]
herb_data $treatment <- factor(herb_data $treatment)
levels(herb_data$treatment)
str(herb_data)
##Hedge's g, overall and aggregated
data_nona<-subset(herb_data, g_overallacross!="NA")
data_nona $treatment <- factor(data_nona $treatment)
data_nona $trait <- factor(data_nona $trait)
data_nona $exposure <- factor(data_nona $exposure)
## standardize variables
data_nona $elev<-scale(data_nona $elevation,center=TRUE, scale=TRUE)
data_nona $lat<-scale(data_nona $latitude,center=TRUE, scale=TRUE)
data_nona $long<-scale(data_nona $longitude,center=TRUE, scale=TRUE)
data_nona $year<-scale(data_nona $pub_year,center=TRUE, scale=TRUE)
# Convert the 'treatment' and 'trait' variables to numeric
data_nona$your_variable_treatment <- as.numeric(data_nona$treatment)
data_nona$your_variable_trait <- as.numeric(data_nona$trait)
# Check the structure and summary of data_nona before conversion
str(data_nona)
summary(data_nona)
# Convert variables to numeric
data_nona$your_variable_treatment <- as.numeric(data_nona$treatment)
data_nona$your_variable_trait <- as.numeric(data_nona$trait)
# Check the structure and summary of data_nona after conversion
str(data_nona)
summary(data_nona)
# Check data types
cat("Data types:\n")
cat("your_variable_treatment: ", class(data_nona$your_variable_treatment), "\n")
cat("your_variable_trait: ", class(data_nona$your_variable_trait), "\n")
# Create a new variable representing the interaction
data_nona$interaction_var <- data_nona$your_variable_treatment * data_nona$your_variable_trait
# Calculate the V matrix
V_matrix <- vcv(tree2)
# Find the species in your dataset that are also in the phylogenetic tree
matching_species <- intersect(data_nona$Species, tree2$tip.label)
# Create a new dataset with only the matching species
filtered_data <- data_nona[data_nona$Species %in% matching_species, ]
# Verify that the filtered dataset contains only the matching species
unique_species_in_filtered_data <- unique(filtered_data$Species)
print(unique_species_in_filtered_data)
# Check for missing values
if (any(is.na(filtered_data$interaction_var))) {
cat("There are missing values in the interaction variable.\n")
} else {
# Specify the formula using the new interaction variable and provide the V matrix
final_mod <- rma.mv(yi = filtered_data$interaction_var - 1,
V = V_matrix,
random = list(~1 | PaperID),
data = filtered_data,
struct = "UN",
method = "REML")
cat("Analysis completed successfully.\n")
}
class(g_overallacross)
# Print the structure of the final_mod model object
str(final_mod)
rma.mv expects all your data (i.e. both the yi part and the V part ) to be within what is passed to its data parameter.
in your case you pass filtered_data as the data.
Therefore you need to have filtered_data contain your yi as you want it, and your V as you want it, and in the yi and V paramaters to the rma.mv function you are simply naming which parts of filtered_data are to be used for the yi purpose and which variable to use for the V.