Error in colnames - r

Could anyone help me with some little problem?
When I plot the frontier I get the following message: "Error in colnames<-(tmp, value = c("targetRisk", "targetReturn")) :
attempt to set 'colnames' on an object with less than two dimensions"(see below for detail). How could I solve this. Thanks a lot.
Portfolio construction & Optimisation
Assets: LUTAX, PFODX,BRGAX,GFAFX,NMSAX,EGINX,IPOYX,SCWFX,FGLDX,PAGEX
Getting monthly returns of the assets
library(quantmod)
library(tseries)
library(timeSeries)
LUTAX <- monthlyReturn((getSymbols("LUTAX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(LUTAX) <- c("LUTAX")
PFODX <- monthlyReturn((getSymbols("PFODX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(PFODX) <- c("PFODX")
BRGAX <- monthlyReturn((getSymbols("BRGAX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(BRGAX) <- c("BRGAX")
GFAFX <- monthlyReturn((getSymbols("GFAFX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(GFAFX) <- c("GFAFX")
NMSAX <- monthlyReturn((getSymbols("NMSAX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(NMSAX) <- c("NMSAX")
EGINX <- monthlyReturn((getSymbols("EGINX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(EGINX) <- c("EGINX")
IPOYX <- monthlyReturn((getSymbols("IPOYX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(IPOYX) <- c("IPOYX")
SCWFX <- monthlyReturn((getSymbols("SCWFX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(SCWFX) <- c("SCWFX")
FGLDX <- monthlyReturn((getSymbols("FGLDX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(FGLDX) <- c("FGLDX")
PAGEX <- monthlyReturn((getSymbols("PAGEX",auto.assign=FALSE)[,4]),type = "arithmetic")
colnames(PAGEX) <- c("PAGEX")
Merging returns of the assets (excluding NA's)
portfolio_returns <- merge(LUTAX, PFODX,BRGAX,GFAFX,NMSAX,EGINX,IPOYX,SCWFX,FGLDX,PAGEX,all=F)
data <- as.timeSeries(portfolio_returns)
Optimisation portfolio
library(fPortfolio)
spec <- portfolioSpec()
setNFrontierPoints <- 25
setSolver(spec) <- "solveRquadprog"
constraints <- c("minW[1:1]=0.12","maxW[1:1]=0.18","minW[2:2]=0.12","maxW[2:2]=0.18",
"minW[3:3]=0.10","maxW[3:3]=0.15","minW[4:4]=0.08","maxW[4:4]=0.12",
"minW[5:5]=0.08","maxW[5:5]=0.12","minW[6:6]=0.05","maxW[6:6]=0.10",
"minW[7:7]=0.05","maxW[7:7]=0.10","minW[8:8]=0.08","maxW[8:8]=0.12",
"minW[9:9]=0.05","maxW[9:9]=0.10","minW[10:10]=0.08","maxW[10:10]=0.12",
"minsumW[c(1:1,2:2)]=0.27","maxsumW[c(1:1,2:2)]=0.33",
"minsumW[c(3:3,4:4,6:6,10:10)]=0.37","maxsumW[c(3:3,4:4,6:6,10:10)]=0.43",
"minsumW[c(5:5,7:7,8:8,9:9)]=0.27","maxsumW[c(5:5,7:7,8:8,9:9)]=0.33",
"maxsumW[c(1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10)]=1")
portfolioConstraints(data,spec,constraints)
frontier<- portfolioFrontier(data,spec,constraints)
print(frontier)
tailoredFrontierPlot(frontier)
After running the last command above I get the following message: "Error in colnames<-(tmp, value = c("targetRisk", "targetReturn")) :
attempt to set 'colnames' on an object with less than two dimensions"

Related

How do I resolve an integration error in Seurat?

I am new to Seurat, and am trying to run an integrated analysis of two different single-nuclei RNAseq datasets. I have been following the Seurat tutorial on integrated analysis (https://satijalab.org/seurat/articles/integration_introduction.html) to guide me, but when I ran the last line of code, I got an error.
# Loading required libraries
library(Seurat)
library(cowplot)
library(patchwork)
# Set up the Seurat Object
vgat.data <- Read10X(data.dir = "~/Desktop/VGAT Viral Data 1/")
vglut.data <- Read10X(data.dir = "~/Desktop/VGLUT3 Viral/")
# Initialize the Seurat object with the raw (non-normalized data)
vgat <- CreateSeuratObject(counts = vgat.data, project = "VGAT/VGLUT Integration", min.cells = 3, min.features = 200)
vglut <- CreateSeuratObject(counts = vglut.data, project = "VGAT/VGLUT Integration", min.cells = 3, min.features = 200)
# Merging the datasets
vgat <- AddMetaData(vgat, metadata = "VGAT", col.name = "Cell")
vglut <- AddMetaData(vglut, metadata = "VGLUT", col.name = "Cell")
merged <- merge(vgat, y = vglut, add.cell.ids = c("VGAT", "VGLUT"), project = "VGAT/VGLUT Integration")
# Split the dataset into a list of two seurat objects (vgat and vglut)
merged.list <- SplitObject(merged, split.by = "Cell")
# Normalize and Identify variable features for each dataset independently
merged.list <lapply(X = merged.list, FUN = function(x) {
x <- NormalizeData(x)
x <- FindVariableFeatures(x, selection.method = "vst", nFeatures = 2000)
})
After running the last line of code, I get the following error: Error in merged.list < lapply(X = merged.list, FUN = function(x) { :
comparison of these types is not implemented
I was wondering if anyone is familiar with Seurat and knows how I can troubleshoot this error. Any help would be greatly appreciated.

KNN: "no missing values are allow" -> I do not have missing values

I am in a group project for a class and one of the people in my group ran the normalization, as well as creating the test/train sets so that we all have the same sets to work from (we're all utilizing different algorithms). I am assigned with running the KNN algorithm.
We had multiple columns with NA's so those columns were omitted (<-NULL). When attempting to run the KNN I keep getting the error of
Error in knn(train = trainsetne, test = testsetne, cl = ne_train_target, :
no missing values are allowed
I ran which(is.na(dataset$col)) and found:
which(is.na(testsetne$median_days_on_market))
# [1] 8038 8097 8098 8100 8293 8304
When I look through the dataset those cells do not have missing data.
I am wondering if I may get some help with how to either find and fix the "No missing values" or to find a work around (if any).
I am sorry if I am missing something simple. Any help is appreciated.
I have listed the code that we have below:
ne$pending_ratio_yy <- ne$total_listing_count_yy <- ne$average_listing_price_yy <- ne$median_square_feet_yy <- ne$median_listing_price_per_square_feet_yy <- ne$pending_listing_count_yy <- ne$price_reduced_count_yy <- ne$median_days_on_market_yy <- ne$new_listing_count_yy <- ne$price_increased_count_yy <- ne$active_listing_count_yy <- ne$median_listing_price_yy <- ne$flag <- NULL
ne$pending_ratio_mm <- ne$total_listing_count_mm <- ne$average_listing_price_mm <- ne$median_square_feet_mm <- ne$median_listing_price_per_square_feet_mm <- ne$pending_listing_count_mm <- ne$price_reduced_count_mm <- ne$price_increased_count_mm <- ne$new_listing_count_mm <- ne$median_days_on_market_mm <- ne$active_listing_count_mm <- ne$median_listing_price_mm <- NULL
ne$factor_month_date <- as.factor(ne$month_date_yyyymm)
ne$factor_median_days_on_market <- as.factor(ne$median_days_on_market)
train20ne= sample(1:20893, 4179)
trainsetne=ne[train20ne,1:10]
testsetne=ne[-train20ne,1:10]
#This is where I start to come in
ne_train_target <- ne[train20ne, 3]
ne_test_target <- ne[-train20ne, 3]
predict_1 <- knn(train = trainsetne, test = testsetne, cl=ne_train_target, k=145)
# Error in knn(train = trainsetne, test = testsetne, cl = ne_train_target, :
# no missing values are allowed

movielense popularity recommender code with R

I'm now studying R, and now doing project about movie recommend algorithm.
I used movielense 100k data with recommenderlab library, and use these tutorials.
https://mitxpro.mit.edu/asset-v1%3AMITProfessionalX+DSx+2017_T1+type#asset+block#Module4_CS1_Movies.pdf
https://cran.r-project.org/web/packages/recommenderlab/vignettes/recommenderlab.pdf
I've now calculated sparsity, and splited data into train and test data.
And I want to make popularity recommendation code. My code is here:
install.packages("SnowballC")
install.packages("class")
install.packages("dbscan")
install.packages("proxy")
install.packages("recommenderlab")
install.packages("dplyr")
install.packages("tm")
install.packages("reshape2")
library(recommenderlab)
library(dplyr)
library(tm)
library(SnowballC)
library(class)
library(dbscan)
library(proxy)
library(reshape2)
#read data
data<- read.table('C:/Users/ginny/OneDrive/Documents/2018_1/dataanalytics/실습3/ml-100k/u.data')
#####raw data to matrix#####
data.frame2matrix = function(data, rowtitle, coltitle, datatitle,
rowdecreasing = FALSE, coldecreasing = FALSE,
default_value = NA) {
# check, whether titles exist as columns names in the data.frame data
if ( (!(rowtitle%in%names(data)))
|| (!(coltitle%in%names(data)))
|| (!(datatitle%in%names(data))) ) {
stop('data.frame2matrix: bad row-, col-, or datatitle.')
}
# get number of rows in data
ndata = dim(data)[1]
# extract rownames and colnames for the matrix from the data.frame
rownames = sort(unique(data[[rowtitle]]), decreasing = rowdecreasing)
nrows = length(rownames)
colnames = sort(unique(data[[coltitle]]), decreasing = coldecreasing)
ncols = length(colnames)
# initialize the matrix
out_matrix = matrix(NA,
nrow = nrows, ncol = ncols,
dimnames=list(rownames, colnames))
# iterate rows of data
for (i1 in 1:ndata) {
# get matrix-row and matrix-column indices for the current data-row
iR = which(rownames==data[[rowtitle]][i1])
iC = which(colnames==data[[coltitle]][i1])
# throw an error if the matrix entry (iR,iC) is already filled.
if (!is.na(out_matrix[iR, iC])) stop('data.frame2matrix: double entry in data.frame')
out_matrix[iR, iC] = data[[datatitle]][i1]
}
# set empty matrix entries to the default value
out_matrix[is.na(out_matrix)] = default_value
# return matrix
return(out_matrix)
}
#data 열 별로 이름 지정('' 안은 필요에 따라 변경 가능)
colnames(data)<-c('user_id','item_id','rating','timestamp')
#raw 데이터를 matrix로 변환
pre_data = data.frame2matrix(data, 'user_id', 'item_id', 'rating')
#matrix를 realratingmatrix로 변환
target_data<- as(as(pre_data, "matrix"), "realRatingMatrix")
data=data[,-which(names(data) %in% c("timestamp"))]
data
str(data)
summary(data)
hist(data$rating)
write.csv(data,"C:/Users/ginny/OneDrive/Documents/2018_1/dataanalytics/실습
3/u.csv")
Number_Ratings=nrow(data)
Number_Ratings
Number_Movies=length(unique(data$item_id))
Number_Movies
Number_Users=length(unique(data$user_id))
Number_Users
data1=data[data$user_id %in% names(table(data$user_id))
[table(data$user_id)>50],]
Number_Ratings1=nrow(data1)
Number_Movies1=length(unique(data1$item_id))
Number_Users1=length(unique(data1$user_id))
sparsity=((Number_Ratings1)*3*5*100)/((Number_Movies1)*(Number_Users1))
sparsity
install.packages("caTools")
library(caTools)
set.seed(10)
sample=sample.split(data1$rating, SplitRatio=0.75)
train=subset(data1, sample==TRUE)
test=subset(data1, sample==FALSE)
data2<-as.data.frame(data1)
data2
#matrix to realratingmatrix
target_data2<- as(as(pre_data2, "matrix"), "realRatingMatrix")
recommender_models<-recommenderRegistry$get_entry(dataType =
"realRatingMatrix")
recomm_model <- Recommender(data2$rating, method = "POPULAR")
I used data2 realRatingMatrix, but when I run last line, error like this happen:
Error in (function (classes, fdef, mtable) : unable to find an
inherited method for function ‘Recommender’ for signature ‘"integer"’
Can anybody help me what's wrong with it?

R -> Error in `row.names<-.data.frame`

Following this other question (Get p-value about contrast hypothesis for rectangular matrix) I am trying to run the following code in R, but the line:
colnames(posmat) <- "pos_c1"
causes an error when calling the function summary().
Error in `row.names<-.data.frame`(`*tmp*`, value = value) :
duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique value when setting 'row.names': ‘Pos’
Does anybody knows why this error comes up?
Here the MWE:
library(lme4)
library(lmerTest)
library(corpcor)
database <- data.frame(
Clos=factor(c(4,4,1,4,4,3,2,1,2,1,2,2,4,3,1,2,1,4,1,3,2,2,4,4,4,4,2,1,4,2,2,1,4,2,4,2,1,4,4,3)),
Pos=factor(c(2,4,1,2,5,6,7,2,2,2,5,6,3,3,3,8,5,3,4,2,1,4,3,3,2,6,1,8,3,7,5,7,8,3,6,6,1,6,3,7)),
RF=c(8,6,2,9,7,1,7,6,3,4,6,4,5,2,5,5,3,4,1,3,1,2,3,1,2,2,3,1,8,5,2,2,7,1,9,4,5,6,4,2),
Score=c(4,3,3,5,4,3,2,4,5,2,2,3,3,4,4,4,3,2,3,3,5,4,3,4,4,2,3,4,3,4,1,2,2,2,3,4,5,3,1,2)
)
clos_c1 = c(0,0,-1,1)
clos_c2 = c(0,-1,0,1)
clos_c3 = c(-1,0,0,1)
closmat.temp = rbind(constant = 1/4,clos_c1,clos_c2,clos_c3)
closmat = solve(closmat.temp)
closmat = closmat[, -1]
closmat
pos_c1 = c(1/2,1/2,-1/6,-1/6,-1/6,-1/6,-1/6,-1/6)
posmat.temp = rbind(pos_c1)
posmat = pseudoinverse(posmat.temp)
colnames(posmat) <- "pos_c1"
contrasts(database$Clos) = closmat
contrasts(database$Pos) = posmat
model = lmer(Score~Clos+Pos+(1|RF), data = database, REML = TRUE)
summary(model)
The problem is that when you run the model, you have the contrasts(database$Pos) without colnames but just one.
You can see that by running your model variable and you will see 6 variables with the name "Pos". This causes trouble in reading the summary() command. Just by adding the line
colnames(contrasts(database$Pos))<-c("pos1","pos2","pos3","pos4","pos5","pos6","pos7")
after the creation of your contrasts(database$Pos) <- posmat
your code will work. Feel free to put the colnames you require.
The whole code is as follows then:
library(lme4)
library(lmerTest)
library(corpcor)
database <- data.frame(
Clos=factor(c(4,4,1,4,4,3,2,1,2,1,2,2,4,3,1,2,1,4,1,3,2,2,4,4,4,4,2,1,4,2,2,1,4,2,4,2,1,4,4,3)),
Pos=factor(c(2,4,1,2,5,6,7,2,2,2,5,6,3,3,3,8,5,3,4,2,1,4,3,3,2,6,1,8,3,7,5,7,8,3,6,6,1,6,3,7)),
RF=c(8,6,2,9,7,1,7,6,3,4,6,4,5,2,5,5,3,4,1,3,1,2,3,1,2,2,3,1,8,5,2,2,7,1,9,4,5,6,4,2),
Score=c(4,3,3,5,4,3,2,4,5,2,2,3,3,4,4,4,3,2,3,3,5,4,3,4,4,2,3,4,3,4,1,2,2,2,3,4,5,3,1,2)
)
clos_c1 = c(0,0,-1,1)
clos_c2 = c(0,-1,0,1)
clos_c3 = c(-1,0,0,1)
closmat.temp = rbind(constant = 1/4,clos_c1,clos_c2,clos_c3)
closmat = solve(closmat.temp)
closmat = closmat[, -1]
closmat
pos_c1 = c(1/2,1/2,-1/6,-1/6,-1/6,-1/6,-1/6,-1/6)
posmat.temp = rbind(pos_c1)
posmat <- pseudoinverse(posmat.temp)
colnames(posmat) <- "pos_c1"
contrasts(database$Clos) <- closmat
contrasts(database$Pos) <- posmat
##NEW LINE
colnames(contrasts(database$Pos))<-c("pos1","pos2","pos3","pos4","pos5","pos6","pos7")
model <- lmer(Score~Clos+Pos+(1|RF), data = database, REML = TRUE)
summary(model)
I hope it helps. Cheers!

Getting error while creating R markdown PDF report

I am getting an error while creating a PDF report out of R markdown file. Below is the snippet of the error:
Error in --dayBikeData <- read.csv("D:\\Madhav\\Study\\MSIS\\PredictiveLearning\\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv") :
object 'dayBikeData' not found
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
I have this object -dayBikeData in the session but still it is giving the error don't know how to proceed on this.
Code for fetching the data from the csv file:
```{r}
dayBikeData <- read.csv("D:\\Madhav\\Study\\MSIS\\PredictiveLearning
\\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv")
# Performs each of the operation asked in the question
basicOperations <- function(inputData){
lenData <- length(inputData)
avg <- round(mean(inputData, na.rm = TRUE), digits = 2) # mean calculation
standardDeviation <- round(sd(inputData), digits = 2) # Standard deviation
sem <- round(standardDeviation/sqrt(lenData), digits = 2)
# Formula for CI is mean - error where error is
error = round(qnorm(0.975)*standardDeviation/sqrt(lenData), digits = 2)
lower_ci <- avg - error
upper_ci <- avg + error
# resultList <- list(obs = lenData, mean = avg, standarDeviation = sd,
# standardMeanError= sem, lowerCI = lower_ci, upperCI = upper_ci
resultList <- c(lenData, avg, standardDeviation, sem,lower_ci,upper_ci)
print(resultList)
}
#Calculations for the Year Wise Data
# dData2011 <- dayBikeData[dayBikeData$yr==0,]
# dData2012 <- dayBikeData[dayBikeData$yr==1,]
dData2011ResultSet <- basicOperations(dayBikeData[dayBikeData$yr==0,]$cnt)
dData2012ResultSet <- basicOperations(dayBikeData[dayBikeData$yr==1,]$cnt)
#Calculations for the Holiday Wise Data
# dDataHoliady_0 <- dayBikeData[dayBikeData$holiday ==0,]
# dDataHoliady_1 <- dayBikeData[dayBikeData$holiday ==1,]
dDataHoliady0ResultSet <- basicOperations(dayBikeData[dayBikeData$holiday ==0,]$cnt)
dDataHoliady1ResultSet <- basicOperations(dayBikeData[dayBikeData$holiday ==1,]$cnt)
#Calculations for the WorkingDay Wise Data
# dDataWorkingDay_0 <- dayBikeData[dayBikeData$workingday ==0,]
# dDataWorkingDay_1 <- dayBikeData[dayBikeData$workingday ==1,]
dDataWorkingDay0ResultSet <- basicOperations(dayBikeData[dayBikeData$workingday ==0,]$cnt)
dDataWorkingDay1ResultSet <- basicOperations(dayBikeData[dayBikeData$workingday ==1,]$cnt)
#Calculations for the Temperature wise data
avgTemp <- mean(dayBikeData$temp, na.rm = TRUE)
dDataTempGreaterEq <- dayBikeData[dayBikeData$temp >= avgTemp,]
dDataTempLess <- dayBikeData[dayBikeData$temp < avgTemp,]
dDataTempGreaterEqResultSet <- basicOperations(dDataTempGreaterEq$cnt)
dDataTempLessResultSet <- basicOperations(dDataTempLess$cnt)
#Calculations for the Weather wise data
# dDataWeather_1 <- dayBikeData[dayBikeData$weathersit ==1,]
# dDataWeather_2 <- dayBikeData[dayBikeData$weathersit ==2,]
# dDataWeather_3 <- dayBikeData[dayBikeData$weathersit ==3,]
dDataWeather1ResultSet <- basicOperations(dayBikeData[dayBikeData$weathersit ==1,]$cnt)
dDataWeather2ResultSet <- basicOperations(dayBikeData[dayBikeData$weathersit ==2,]$cnt)
dDataWeather3ResultSet <- basicOperations(dayBikeData[dayBikeData$weathersit ==3,]$cnt)
#Calculations for the Season wise data
# dDataSeason_1 <- dayBikeData[dayBikeData$season ==1,]
# dDataSeason_2 <- dayBikeData[dayBikeData$season ==2,]
# dDataSeason_3 <- dayBikeData[dayBikeData$season ==3,]
# dDataSeason_4 <- dayBikeData[dayBikeData$season ==4,]
dDataSeason1ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==1,]$cnt)
dDataSeason2ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==2,]$cnt)
dDataSeason3ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==3,]$cnt)
dDataSeason4ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==4,]$cnt)
#Constrcut a row wise data
resultData <- rbind(dData2011ResultSet, dData2012ResultSet, dDataHoliady0ResultSet,
dDataHoliady1ResultSet,dDataWorkingDay0ResultSet,
dDataWorkingDay1ResultSet,dDataTempGreaterEqResultSet,
dDataTempLessResultSet, dDataWeather1ResultSet,
dDataWeather2ResultSet, dDataWeather3ResultSet,dDataSeason1ResultSet,
dDataSeason2ResultSet, dDataSeason3ResultSet,dDataSeason4ResultSet)
colnames(resultData) <- c("N","Mean","SD" , "SEM","Lower_CI", "UPPER_CI")
rownames(resultData) <- c("Year-0", "Year-1", "Holiday-0", "Holiday-1", "WorkingDay-0",
"WorkingDay-1","Temperature >=","Temperature <", "Weather-1",
"Weather-2","Weather-3","Season-1","Season-2", "Season-3",
"Season-4")
df.resultData <- as.data.frame(resultData)
df.resultData["Value"] <- NA
df.resultData$Value <- c(2011, 2012, 0,1, 0,1,1, 0, 1,2,3,1,2,3,4)
df.resultData = df.resultData[,c(7,1,2,3,4,5,6)]
library(knitr)
# print(xtable(df.resultData), type = "latex")
kable(df.resultData, format = "markdown")
write.csv(df.resultData, file = "D:\\X\\Study\\MSIS\\PredictiveLearning\\OutputResult.csv")
Your file path is wrong... There is a new line and lots of spaces in the middle of it.
> "D:\\Madhav\\Study\\MSIS\\PredictiveLearning
+ \\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv"
[1] "D:\\Madhav\\Study\\MSIS\\PredictiveLearning\n \\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv"
So the file is not getting read properly and hence the object is not available in the knitr session.
I downloaded your dataset from UCI Machine Learning Repository, saved your markdown in a new folder, adjusted the filenames by deleting the paths, ran it, and it worked fine.
So I maybe your session is corrupt, or the paths are wrong, or something. Try what I did and it should work.
Proof:

Resources